R
Rohan
Hi everyone
I have couple of question regarding my coding I am going a Server
using TcpClient!
1) Does anyone know why do I get Junk when the client connects
initially? I have to use ByPassStartupWord to bypass the initial junk
2) Is my method of watch Dog to Timer to check if the client has timed
out or disconnected is any good? Or is there any other method of doing
so?
3) Am I using the right method of disposing the Class using the sub
DisposeAndDisconect? It seem that when I use that the CPU goes really
high, not sure if the class has been disposed !
Thank you !
------------------------------------
Imports System.Net
Imports System.Threading
Public Class TCPIP
Dim _Counter As Integer = 0
Public ReadOnly Property NumberofConn()
Get
Return _Counter
End Get
End Property
Public Sub StartServer(Optional ByVal ServerIP As String =
"127.0.0.1")
Dim IpEP As New IPEndPoint(IPAddress.Parse(ServerIP), 35)
Dim SckListener As New Sockets.TcpListener(IpEP)
SckListener.Start(30)
While True
_Counter += 1
Dim SMTPConn As Sockets.TcpClient =
SckListener.AcceptTcpClient()
Dim ClientC As New SMTP(SMTPConn, _Counter)
ThreadPool.QueueUserWorkItem(AddressOf ClientC.Start)
End While
End Sub
End Class
Public Class SMTP
Implements IDisposable
Dim MyClient As Sockets.TcpClient
Dim MyID As Integer = 0
Dim NetStreem As Sockets.NetworkStream
Dim DataBytes(1024) As Byte
Dim ClientDogWatch As New Timers.Timer
Dim WDStatusTimeOut As Boolean = False
Dim LastReceivedData As New Date
Sub New(ByVal SmtpClient As Sockets.TcpClient, ByVal ClientID As
Integer)
MyClient = SmtpClient
MyID = ClientID
End Sub
Public Sub [Start]()
Dim MyData As String = String.Empty
Dim DataLen As Integer = 0
Dim ByPassStartupWord As Boolean = False
NetStreem = MyClient.GetStream
MyClient.ReceiveTimeout = 10000
MyClient.SendTimeout = 10000
ClientDogWatch.Interval = 30000
ClientDogWatch.Start()
'set up Watch dog
AddHandler ClientDogWatch.Elapsed, AddressOf
MyClass.CheckLastRcvTime
Try
While True
If NetStreem.DataAvailable Then
If NetStreem.CanRead Then
'Get Data from sockets
DataLen = NetStreem.Read(DataBytes, 0,
DataBytes.Length)
MyData = System.Text.Encoding.ASCII.GetString
(DataBytes, 0, DataLen).Trim
xMailStatus = MailSatus.RECEIVE_DATA
If MyData.Length > 0 Then
If ByPassStartupWord = False Then
'By pass the initial junk received form socket
ByPassStartupWord = True
Else
Debug.Print(MyID & ":" & MyData)
End If
End If
End If
End If
If Not MyClient.Connected Or WDStatusTimeOut = True
Then
'exit loop upon error
Exit While
End If
End While
'Dispose everything and close Connection with client
Call DisposeAndDisonnect()
Catch ex As Exception
End Try
End Sub
Private Sub CheckLastRcvTime()
'Close Connection when Connection is lost after 30sec
If DateAndTime.DateDiff(DateInterval.Second, LastReceivedData,
Now) > 30 Then
xMailStatus = MailSatus.MAIL_LOST
WDStatusTimeOut = True
DisposeAndDisonnect()
End If
End Sub
Public Sub DisposeAndDisonnect()
ClientDogWatch.Dispose()
NetStreem.Close()
MyClient.Close()
Me.Dispose()
End Sub
Private disposedValue As Boolean = False ' To detect
redundant calls
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
MyClient.Close()
' TODO: free other state (managed objects).
End If
' TODO: free your own state (unmanaged objects).
' TODO: set large fields to null.
End If
Me.disposedValue = True
End Sub
#Region " IDisposable Support "
' This code added by Visual Basic to correctly implement the
disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal
disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
I have couple of question regarding my coding I am going a Server
using TcpClient!
1) Does anyone know why do I get Junk when the client connects
initially? I have to use ByPassStartupWord to bypass the initial junk
2) Is my method of watch Dog to Timer to check if the client has timed
out or disconnected is any good? Or is there any other method of doing
so?
3) Am I using the right method of disposing the Class using the sub
DisposeAndDisconect? It seem that when I use that the CPU goes really
high, not sure if the class has been disposed !
Thank you !
------------------------------------
Imports System.Net
Imports System.Threading
Public Class TCPIP
Dim _Counter As Integer = 0
Public ReadOnly Property NumberofConn()
Get
Return _Counter
End Get
End Property
Public Sub StartServer(Optional ByVal ServerIP As String =
"127.0.0.1")
Dim IpEP As New IPEndPoint(IPAddress.Parse(ServerIP), 35)
Dim SckListener As New Sockets.TcpListener(IpEP)
SckListener.Start(30)
While True
_Counter += 1
Dim SMTPConn As Sockets.TcpClient =
SckListener.AcceptTcpClient()
Dim ClientC As New SMTP(SMTPConn, _Counter)
ThreadPool.QueueUserWorkItem(AddressOf ClientC.Start)
End While
End Sub
End Class
Public Class SMTP
Implements IDisposable
Dim MyClient As Sockets.TcpClient
Dim MyID As Integer = 0
Dim NetStreem As Sockets.NetworkStream
Dim DataBytes(1024) As Byte
Dim ClientDogWatch As New Timers.Timer
Dim WDStatusTimeOut As Boolean = False
Dim LastReceivedData As New Date
Sub New(ByVal SmtpClient As Sockets.TcpClient, ByVal ClientID As
Integer)
MyClient = SmtpClient
MyID = ClientID
End Sub
Public Sub [Start]()
Dim MyData As String = String.Empty
Dim DataLen As Integer = 0
Dim ByPassStartupWord As Boolean = False
NetStreem = MyClient.GetStream
MyClient.ReceiveTimeout = 10000
MyClient.SendTimeout = 10000
ClientDogWatch.Interval = 30000
ClientDogWatch.Start()
'set up Watch dog
AddHandler ClientDogWatch.Elapsed, AddressOf
MyClass.CheckLastRcvTime
Try
While True
If NetStreem.DataAvailable Then
If NetStreem.CanRead Then
'Get Data from sockets
DataLen = NetStreem.Read(DataBytes, 0,
DataBytes.Length)
MyData = System.Text.Encoding.ASCII.GetString
(DataBytes, 0, DataLen).Trim
xMailStatus = MailSatus.RECEIVE_DATA
If MyData.Length > 0 Then
If ByPassStartupWord = False Then
'By pass the initial junk received form socket
ByPassStartupWord = True
Else
Debug.Print(MyID & ":" & MyData)
End If
End If
End If
End If
If Not MyClient.Connected Or WDStatusTimeOut = True
Then
'exit loop upon error
Exit While
End If
End While
'Dispose everything and close Connection with client
Call DisposeAndDisonnect()
Catch ex As Exception
End Try
End Sub
Private Sub CheckLastRcvTime()
'Close Connection when Connection is lost after 30sec
If DateAndTime.DateDiff(DateInterval.Second, LastReceivedData,
Now) > 30 Then
xMailStatus = MailSatus.MAIL_LOST
WDStatusTimeOut = True
DisposeAndDisonnect()
End If
End Sub
Public Sub DisposeAndDisonnect()
ClientDogWatch.Dispose()
NetStreem.Close()
MyClient.Close()
Me.Dispose()
End Sub
Private disposedValue As Boolean = False ' To detect
redundant calls
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
MyClient.Close()
' TODO: free other state (managed objects).
End If
' TODO: free your own state (unmanaged objects).
' TODO: set large fields to null.
End If
Me.disposedValue = True
End Sub
#Region " IDisposable Support "
' This code added by Visual Basic to correctly implement the
disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal
disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class