shutdown()

PROTOTYPE

#include <sockets.h>

int shutdown(int socket, int how);

DESCRIPTION

shutdown() limits use of the socket specified by socket and can initiate the half-close of a TCP connection. For the legal values of how, “sockets.h” defines the POSIX macros SHUT_RD (0), SHUT_WR (1), and SHUT_RDWR (2).

If how is 0, all receive data queued at the socket is dropped. Until the socket is closed or its connection lost (TCP sockets), later recv() and recvfrom() calls return 0. For TCP sockets, the socket receive buffer is freed. If any receive data later arrives from the peer, a RST is sent and the connection is dropped.

If how is 1, later send() and sendto() calls return -1 with errno set to ESHUTDOWN. For TCP sockets, all queued send data is sent (regardless of the Nagle flag setting) followed by a FIN, to notify the peer that no further data will be sent. This is perhaps the most useful option.

If how is 2, the behavior for it being equal to 0 and equal to 1 is combined.

closesocket() must still be called after the socket is no longer needed, to fully recover socket resources. The SO_LINGER socket option, which impacts the behavior of closesocket() for TCP sockets, has no effect on shutdown().

If successful, shutdown() returns 0. Otherwise, it sets errno and returns -1.

ERROR CODES

ECONNABORTED Connection was aborted because of unacknowledged transmissions.
ECONNRESET Connection was forcibly closed by remote host (received RST).
EINVAL The value of how is invalid.
ENETDOWN TargetTCP has not been initialized.
ENOTCONN The socket is unconnected.
ENOTSOCK socket is not a valid socket handle.

EXAMPLE

  /*-------------------------------------------------------------------*/
  /* Shutdown socket for further sends.                                */
  /*-------------------------------------------------------------------*/
  rc = shutdown(sock, SHUT_WR);