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 "looks" 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.
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 "looks" 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.