Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Socket.Disconnect(bool reuse) according to MSDN enables the program to reuse the socket.

My questions are:

1) If we decide to reuse the socket does that mean it will reuse the same local end point as well? What I'm asking is would it preserve the port for that socket or would it just (I don't believe it does) save all of the object's resources ?

2) If in effect it does only save the resources, would they be allocated to the next connection and if so do the send/receive buffers automatically flush or is there some way to flush them? Does Socket.ShutDown(ShutDown.Both) do that for me on disconnect?

3) In what situation would I use Socket.Disconnect(false) because if the purpose is to reuse the socket, then what would this give me?
Posted
Updated 19-Feb-11 2:40am
v3
Comments
Abdul Quader Mamun 18-Feb-11 21:08pm    
Edited spelling.

1 solution

Calling Socket.Disconnect(true) will internally result in Winsock's DisconnectEx function being called with the TF_REUSE_SOCKET parameter.

From the docs:
Prepares the socket handle to be reused. When the DisconnectEx request completes, the socket handle can be passed to the AcceptEx or ConnectEx function.


So it's only the socket handle that will be reused. This has nothing to do with your local endpoint.

Socket reuse is meant for fairly advanced use, where you know exactly what you are doing. It's typically used in high performance heavy traffic TCP servers. The idea being that by reusing a socket, you save some time (the time that would normally have gone into allocating that socket and its resources). So what some server developers do is to have a pre-created socket pool that they pull sockets from as and when required.

So in my opinion, for a client side TCP application there is never a good reason to reuse a socket.

In short, always use Socket.Disconnect(false) unless you really know what you are doing.

For some reason, this is not so well documented and leads to a lot of confusion.
 
Share this answer
 
v3
Comments
Espen Harlinn 19-Feb-11 10:10am    
Good answer :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900