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?
In summary, use shutdown to send a shutdown sequence at the TCP level and use close to free up the resources used by the socket data structures in your process. If you haven't issued an explicit shutdown sequence by the time you call close then one is initiated for you.
Shutdown (in your case) indicates to the other end of the connection there is no further intention to read from or write to the socket. Then close frees up any memory associated with the socket. Omitting shutdown may cause the socket to linger in the OSs stack until the connection has been closed gracefully.
close() call shuts down the socket associated with the socket descriptor socket, and frees resources allocated to the socket. If socket refers to an open TCP connection, the connection is closed.
You need to call shutdown() first and then close(), and shutdown takes an argument.
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.
It is unnecessary and redundant except in the following cases:
These are the only cases I've ever come across in about 30 years: there may be others but I'm not aware of them.
There are no specific resources associated with sending or receiving operation on the socket, the socket is either used or closed. There reason for shutdown is not related to resource-management. Shutting down the socket is implementation of so-called graceful shutdown protocol, which allow both sides of the communication to realize the connection is going down and allows to minimize loss of data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With