Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reconnecting sockets

Status
Not open for further replies.

lorenzodv

Programmer
Feb 16, 2001
95
IT
Hi.

I'm using the Socket class in a network client application to make TCP connections.
I create the socket, bind it to the local endpoint and use the Connect method to start the connection.

When disconnecting, I use the following code:

<SocketObject>.Shutdown(SocketShutdown.Both)
<SocketObject>.Close()

When I try to connect again, using the Connect() method on the same <SocketObject>, an exception arises, saying the object was disposed. I think this is caused by the Close() method.
So I tried creating a new socket each time I connect (since they are disposed when I disconnect this should work... or at least this was what I thought). The problem now is that when I connect for the second time, it says the local endpoint is already in use: how this could be? Wasn't it disposed?

So the problem is: the socket &quot;looks&quot; disposed, but if I try to bind a new one to the same address/port, it seems to still exist (for about 1 minute).

I know there is a solution to this, probably simple.
Could someone tell me the right way to reconnect multiple times with a socket?

Thanks in advance.
 
Once you close a socket, you can not open it again.
You could however do something like this:

'done using the socket, so close it.
MySocket.shutdown(Both)
MySocket.close

'now you need to use it again
MySocket = new Socket(whatever params)
'set a few parameters (like endpoint, blocking, etc...)
'use the socket to connect again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top