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
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