Need help with my TcpListener code.

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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


.



Relevant Pages

  • Re: Beispiel gesucht zu pessimistisches Sperren
    ... Public Shared Sub Tab1RowLock ... Dim cmd As New SqlCommand ... Using connection As New SqlConnection ...
    (microsoft.public.de.german.entwickler.dotnet.datenbank)
  • vb networking problem
    ... I cannot run the client without administrator privileges. ... Dim WithEvents evtApp As New EventLog ... Private response As String = String.Empty ... Public Sub Main ...
    (microsoft.public.dotnet.languages.vb)
  • Re: memory issue using ADO to query Excel
    ... > Northwind does not help ... Sub Test() ... Dim Con As ADODB.Connection ... In a loop, open a connection to northwind, open ...
    (microsoft.public.excel.programming)
  • Re: Updating Access data using SQL / refresh time question
    ... the screen updates for every addition to the listview. ... > really want a server-side cursor), connection and RS retrieval timeouts, ... >> sub Test ... >> dim oconn as new adodb.connection ...
    (microsoft.public.vb.database)
  • Re: Updating Access data using SQL / refresh time question
    ... listview is causing a delay of 5-10 seconds and sometimes longer. ... > really want a server-side cursor), connection and RS retrieval timeouts, ... >> sub Test ... >> dim oconn as new adodb.connection ...
    (microsoft.public.vb.database)