Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# socket
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 17-Feb-11 21:09pm
otzap451
Edited 19-Feb-11 3:40am
v3
Comments
Abdul Quader Mamun at 18-Feb-11 21:08pm
   
Edited spelling.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v3
Comments
Espen Harlinn at 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)

  Print Answers RSS
0 BillWoodruff 330
1 Mathew Soji 309
2 Maciej Los 295
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 217
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,025


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 19 Feb 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100