http://blog.csdn.net/ccnyou/article/details/8913334
Original: http://unliminet.blog.51cto.com/380895/346686
When calling Closesocket to close a socket, So_linger determines how the system handles the data remaining in the socket send queue. There are two ways to handle this: discard or continue sending data to the peer, gracefully closing the connection. In fact, So_linger is not recommended, and in most cases we recommend using the default closing method (the first case in the table below).
The code snippet below shows the linger structure syntax, and the table is the socket behavior for different parameter cases.
- typedef struct Linger {
- U_short L_onoff; //switch, 0 or not 0
- U_short L_linger; //gracefully close the maximum time limit
- } linger;
L_onoff |
L_linger |
Closesocket behavior |
Send Queue |
Underlying behavior |
Zero |
Ignore |
Returns immediately. |
Hold until the send is complete. |
The system takes over the socket and guarantees that the data is sent to the peer. |
Non-Zero |
Zero |
Returns immediately. |
Give up immediately. |
Send the RST packet directly, reset itself immediately, without going through the 2MSL state. The peer receives a reset error number. |
Non-Zero |
Non-Zero |
Block until the L_linger time expires or the data is sent to completion. (Sockets must be set to block Zhuan) |
Keep trying to send within the time-out period, and discard it immediately if it expires. |
Timeout is the same as the second case, if the transmission is complete, happy. |
Information available for reference:
http://msdn.microsoft.com/en-us/library/ms737582 (v=vs.85). aspx
http://msdn.microsoft.com/en-us/library/ms739165 (v=vs.85). aspx
Http://blog.csdn.net/factor2000/archive/2009/02/23/3929816.aspx
So_linger and Graceful close connection