On this MSDN page:
Sending and Receiving Data on the Client
It recommends closing the sending side of the socket by using:
shutdown(SOCK_ID, SD_SEND);
Why should I?
Maybe I dont have to, and its just a recommendation? Maybe its for saving memory? Maybe for speed?
Does anyone have an idea?
The answer is in the shutdown()
documentation:
If the how parameter is
SD_SEND
, subsequent calls to thesend
function are disallowed. For TCP sockets, aFIN
will be sent after all data is sent and acknowledged by the receiver....
To assure that all data is sent and received on a connected socket before it is closed, an application should use
shutdown
to close connection before callingclosesocket
. One method to wait for notification that the remote end has sent all its data and initiated a graceful disconnect uses theWSAEventSelect
function as follows :
- Call
WSAEventSelect
to register forFD_CLOSE
notification.- Call
shutdown
withhow=SD_SEND
.- When
FD_CLOSE
received, call therecv
orWSARecv
until the function completes with success and indicates that zero bytes were received. IfSOCKET_ERROR
is returned, then the graceful disconnect is not possible.- Call
closesocket
.Another method to wait for notification that the remote end has sent all its data and initiated a graceful disconnect uses overlapped receive calls follows :
- Call
shutdown
withhow=SD_SEND
.- Call
recv
orWSARecv
until the function completes with success and indicates zero bytes were received. IfSOCKET_ERROR
is returned, then the graceful disconnect is not possible.- Call
closesocket
....
For more information, see the section on Graceful Shutdown, Linger Options, and Socket Closure.
In other words, at least for TCP, calling shutdown(SD_SEND)
notifies the peer that you are done sending any more data, and that you will likely be closing your end of the connection soon. Preferably, the peer will also do the same courtesy for you. This way, both peers can know the connection was closed intentionally on both ends. This is known as a graceful disconnect, and not an abortive or abnormal disconnect.
By default, if you do not call shutdown(SD_SEND)
, closesocket()
will attempt to perform a graceful shutdown for you UNLESS the socket's linger option is disabled. It is best not to rely on this behavior, you should always call shutdown()
yourself before calling closesocket()
, unless you have good reason not to.