Receive Error: Unable to Read Data From The Transport Connection

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

Gineer

I am having exactly the same problem. The interesting part is that I
wrote a app that works like a chat app with a sender and a receiver
class that work perfectly. When I now impliment the receiver class into
the enterprise solution we are building I get this error. On the same
machines, with the same configuration.

What's up with that?

I have disabled Windows Firewall on both machines. Could it be a
security issue?

Very dodgy! Any help would be much apreciated.

Thanks
David
 

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