Need help with my TcpListener code.

T

Terry Olsen

Hoping someone can help me here. I've got this code written, and it works
fine for the first connection. But if I connect another client (while the
first is still connected), I get connected but nothing else, no data
exchange. If I disconnect the 2nd session, I get a System.IO.IOException.
If I disconnect the first session with the 2nd session still connected, then
all the data I typed in the 2nd session gets returned to me as if it was
working fine the whole time (like it was buffered or something).

Class Server

Public Sub StartServer()
Dim SmtpListener As New TcpListener(IpAddr, Port)
SmtpListener.Start(10)
SmtpListener.BeginAcceptTcpClient(AddressOf AcceptCallback, SmtpListener)
End Sub

Private Sub AcceptCallback(ByVal ar As IAsyncResult)
Dim SmtpListener As TcpListener = CType(ar.AsyncState, TcpListener)
Dim client As TcpClient = SmtpListener.EndAcceptTcpClient(ar)
Dim Handler As New ClientHandler(client)
Handler.Start()
SmtpListener.BeginAcceptTcpClient(AddressOf AcceptCallback, SmtpListener)
End Sub

Private Class ClientHandler
Dim sw As StreamWriter
Dim sr As StreamReader
Dim SmtpClient as TcpClient

Public Sub New(ByVal Client As TcpClient)
SmtpClient=Client
sw = New StreamWriter(SmtpClient.GetStream)
sr = New StreamReader(SmtpClient.GetStream)
End Sub

Public Sub Start()
Write2Client("Welcome to the server.")
While sr.EndOfStream = False
Write2Client("Received: " & sr.ReadLine)
End While
Write2Client("Connection Closed")
SmtpClient.Close
End Sub

Private Sub Write2Client(ByVal msg As String)
sw.WriteLine(msg)
sw.Flush()
End Sub
End Class

End Class
 
B

Bart Mermuys

Hi,

Terry Olsen said:
Hoping someone can help me here. I've got this code written, and it works
fine for the first connection. But if I connect another client (while the
first is still connected), I get connected but nothing else, no data
exchange. If I disconnect the 2nd session, I get a System.IO.IOException.

You can get Exception's when for example the connection wasn't closed
gracefully or some error happened, so you should wrap all socket functions
inside a try-catch, when an Exception is thrown you may assume the
connection is closed.
If I disconnect the first session with the 2nd session still connected,
then all the data I typed in the 2nd session gets returned to me as if it
was working fine the whole time (like it was buffered or something).

Well, you are using the asynchronous function BeginAccept, but once a Socket
is accepted you start using synchronous operations like
StreamReader.ReadLine, so your program is in a while loop for one socket
until the connection is closed.

Two possible solutions :
1) Use a thread for each accepted socket, pretty easy because the logic
remains the same and you can keep using StreamReader/StreamWriter.
See modifications to your code for this.

2) Use BeginRead and BeginWrite, but then i gets more complicated because
you may need to mantain state (or buffer) between calls and you can't use
StreamReader/StreamWriter.
Class Server

Public Sub StartServer()
Dim SmtpListener As New TcpListener(IpAddr, Port)
SmtpListener.Start(10)
SmtpListener.BeginAcceptTcpClient(AddressOf AcceptCallback, SmtpListener)
End Sub

Private Sub AcceptCallback(ByVal ar As IAsyncResult)
Dim SmtpListener As TcpListener = CType(ar.AsyncState, TcpListener)
Dim client As TcpClient = SmtpListener.EndAcceptTcpClient(ar)
Dim Handler As New ClientHandler(client)
Handler.Start()
SmtpListener.BeginAcceptTcpClient(AddressOf AcceptCallback, SmtpListener)
End Sub

Private Class ClientHandler
Dim sw As StreamWriter
Dim sr As StreamReader
Dim SmtpClient as TcpClient Dim thread As Thread

Public Sub New(ByVal Client As TcpClient)
SmtpClient=Client
sw = New StreamWriter(SmtpClient.GetStream)
sr = New StreamReader(SmtpClient.GetStream)
End Sub
Public Sub Start
thread = new Thread( AddressOf ThreadProc )
thread.Start()
End Sub

Private Sub ThreadProc()
Write2Client("Welcome to the server.")
While sr.EndOfStream = False
Write2Client("Received: " & sr.ReadLine)
End While
Write2Client("Connection Closed")
SmtpClient.Close
End Sub

Private Sub Write2Client(ByVal msg As String)
sw.WriteLine(msg)
sw.Flush()
End Sub
End Class

End Class

HTH,
Greetings
 
T

Terry Olsen

I get it. My program never gets past the "Handler.Start" line in the
AcceptCallback routine until the session is terminated. I assumed that
using the BeginAcceptTcpClient call would spawn a new thread and that other
things could happen. Thanks for making the lightbulb come on! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top