G
Guest
I'm trying to create a small messenger program that uses the tcpclient and
tcplistenter objects. When I start the application and run the thread that
fires the tcplistener; once the client sends data then closes the stream and
the connection I receive the message Unable to read data from the transport
connection: An existing connection was forcibly closed by the remote host.
How can I keep the connection listener listening for new traffic?
Here is the code I have:
---------Button Click Event-----------------------------------
Dim t1 As New Thread(AddressOf StartListening)
If btnStartListening.Text = "Start Listening" Then
t1.Name = "ListenThread"
t1.IsBackground = True
t1.Start()
ElseIf btnStartListening.Text = "Stop Listening" Then
t1.Abort()
StopListening()
Dim server As TcpListener
server = Nothing
------------------------------------------------------------------------
Private Sub StartListening()
Try
server = New TcpListener(ipLocalEndPoint)
server.Start() 'Start listening for client request.
Dim showProgress As New ChangeFormStatus(AddressOf ChangeStatus)
'Inform user that the app is now listening.
Me.Invoke(showProgress, status.ONLINE)
Dim client As TcpClient
Dim Bufferbytes(1024) As Byte 'Buffer for reading data.
Dim incomingMessage As String = Nothing 'Variable for incoming
message
Dim showMessage As New DisplayText(AddressOf WriteToScreen)
While True
client = server.AcceptTcpClient() 'Perform blocking call to
accept requests.
Me.Invoke(showMessage, "Connection established....")
'Get a stream object for reading and writing.
Dim stream As NetworkStream = client.GetStream()
Dim intBytes As Int32
intBytes = stream.Read(Bufferbytes, 0, Bufferbytes.Length)
While (intBytes <> 0)
'Translate data bytes to a ASCII string
incomingMessage =
System.Text.Encoding.ASCII.GetString(Bufferbytes, 0, Bufferbytes.Length)
Me.Invoke(showMessage, String.Format("Received message
from client: {0}", incomingMessage.ToString()))
Dim msg As Byte() =
System.Text.Encoding.ASCII.GetBytes(incomingMessage)
stream.Write(msg, 0, msg.Length)
intBytes = stream.Read(Bufferbytes, 0, Bufferbytes.Length)
End While
'shutdown the connection
client.Close()
End While
End Sub
I plan to put the server.Close() command under the Click the event for a
button and declare TcpListener as a global object.
tcplistenter objects. When I start the application and run the thread that
fires the tcplistener; once the client sends data then closes the stream and
the connection I receive the message Unable to read data from the transport
connection: An existing connection was forcibly closed by the remote host.
How can I keep the connection listener listening for new traffic?
Here is the code I have:
---------Button Click Event-----------------------------------
Dim t1 As New Thread(AddressOf StartListening)
If btnStartListening.Text = "Start Listening" Then
t1.Name = "ListenThread"
t1.IsBackground = True
t1.Start()
ElseIf btnStartListening.Text = "Stop Listening" Then
t1.Abort()
StopListening()
Dim server As TcpListener
server = Nothing
------------------------------------------------------------------------
Private Sub StartListening()
Try
server = New TcpListener(ipLocalEndPoint)
server.Start() 'Start listening for client request.
Dim showProgress As New ChangeFormStatus(AddressOf ChangeStatus)
'Inform user that the app is now listening.
Me.Invoke(showProgress, status.ONLINE)
Dim client As TcpClient
Dim Bufferbytes(1024) As Byte 'Buffer for reading data.
Dim incomingMessage As String = Nothing 'Variable for incoming
message
Dim showMessage As New DisplayText(AddressOf WriteToScreen)
While True
client = server.AcceptTcpClient() 'Perform blocking call to
accept requests.
Me.Invoke(showMessage, "Connection established....")
'Get a stream object for reading and writing.
Dim stream As NetworkStream = client.GetStream()
Dim intBytes As Int32
intBytes = stream.Read(Bufferbytes, 0, Bufferbytes.Length)
While (intBytes <> 0)
'Translate data bytes to a ASCII string
incomingMessage =
System.Text.Encoding.ASCII.GetString(Bufferbytes, 0, Bufferbytes.Length)
Me.Invoke(showMessage, String.Format("Received message
from client: {0}", incomingMessage.ToString()))
Dim msg As Byte() =
System.Text.Encoding.ASCII.GetBytes(incomingMessage)
stream.Write(msg, 0, msg.Length)
intBytes = stream.Read(Bufferbytes, 0, Bufferbytes.Length)
End While
'shutdown the connection
client.Close()
End While
End Sub
I plan to put the server.Close() command under the Click the event for a
button and declare TcpListener as a global object.