TCPClient

ZeroEffect

Junior Contributor
Joined
Oct 24, 2004
Messages
204
Location
Detroit, MI
Here is what I am trying to do. I am using TCPClient to connect to another application send data and close the connection. Then when more data is is received to reconnect and send data to the same application. I can get this to work once. then I get this message.

Visual Basic:
Once the socket had been disconnected, you can only reconnect again asynchronously,
and only to a different EndPoint. BeginConnect must be called on a thread
that won't exit until the operation has been completed

This is the code I am using.

Visual Basic:
'This is created in another part of the application
'
Dim groupEP As New IPEndPoint(IPAddress.Any, 20000)
tRemote1 = New TcpClient(groupEP)
'
'This is called from another sub.
'
tRemote1.Client.Connect(strRemotePort1IP, strRemotePort1Port)
tRemote1.GetStream.Write(sendBytes1, 0, sendBytes1.Length)
tRemote1.Client.Disconnect(True)
tRemote1.Client.Shutdown(Net.Sockets.SocketShutdown.Both)

My thought is that I shouldn't have to recreate the object everytime I use it. I should be able to just open and close it or am I mistaken.

Thanks for you help,

ZeroEffect
 
I think you will need to create a new client for each connection - TCP is a connection orientated protocol and the TcpClient reflects that in how it operates.

Is there a reason you can't maintain the connection and need to keep disconnecting and reconnecting? If you do not require permanent connection would UDP be a better option?
 
The idea would be to allow the user the option of keeping the connection alive or letting it disconnect.

Thanks for the information.

ZeroEffect
 
Back
Top