By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,087 Members | 1,513 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,087 IT Pros & Developers. It's quick & easy.

Receive Error: Unable to Read Data From The Transport Connection

P: n/a
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(incomingMessag e)
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.

--
TC
Oct 27 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

Terrance wrote:
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(incomingMessag e)
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.

--
TC
Nov 24 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.