Some Question about Socket TCPclient

Some Question about Socket TCPclient

Post by Roha » Mon, 12 Oct 2009 09:17:02


i 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
 
 
 

Some Question about Socket TCPclient

Post by Michael D. » Mon, 12 Oct 2009 09:22:19

Rohan" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...


You need to take a look at the async protocols in dotNET. I suspect they'll
solve your startup problems. Also, framework 2.0 has a SMTP client class
built in at System.Net.Mail.

Mike.



 
 
 

Some Question about Socket TCPclient

Post by Zulande » Mon, 12 Oct 2009 09:43:03

Hi Mike,
About the SMTP, its actually a POP3 server ! i will look in to the
Async
thank you


On Oct 10, 8:22m, "Michael D. Ober" <obermd.@.alum.mit.edu.nospam.>
 
 
 

Some Question about Socket TCPclient

Post by Michael D. » Mon, 12 Oct 2009 12:55:21


Hi Mike,
About the SMTP, its actually a POP3 server ! i will look in to the
Async
thank you


On Oct 10, 8:22 pm, "Michael D. Ober" <obermd.@.alum.mit.edu.nospam.>



You definitely want to take a look at the async listen and accept calls for
a server. The class you want is System.Net.Sockets.Socket. The TCPListener
class uses blocking calls only whereas the Socket class can use either.
Also, take a look at http://www.yqcomputer.com/
for an Asynchronous Server Socket Example.

Mike.