WSPCloseSocket

The WSPCloseSocket function closes a socket.

int WSPCloseSocket (
  SOCKET s,      
  LPINT lpErrno  
);
 

Parameters

s
[in] A descriptor identifying a socket.
lpErrno
[out] A pointer to the error code.

Remarks

This function closes a socket. More precisely, it releases the socket descriptor s, so that further references to s should fail with the error WSAENOTSOCK. If this is the last reference to an underlying socket, the associated naming information and queued data are discarded. Any blocking or asynchronous calls pending on the socket (issued by any thread in this process) are canceled without posting any notification messages. Any pending overlapped operations issued by any thread in this process are also canceled. Whatever completion action was specified for these overlapped operations is performed (for example, event, completion routine, or completion port). In this case, the pending overlapped operations fail with the error status WSA_OPERATION_ABORTED. FD_CLOSE will not be posted after WSPCloseSocket is called.

WSPClosesocket behavior is summarized as follows:

The semantics of WSPCloseSocket are affected by the socket options SO_LINGER and SO_DONTLINGER as follows:

Option Interval Type of close Wait for close?
SO_DONTLINGER Do not care Graceful No
SO_LINGER Zero Hard No
SO_LINGER Nonzero Graceful Yes

If SO_LINGER is set (that is, the l_onoff field of the linger structure is nonzero) and the time-out interval, l_linger, is zero, WSPClosesocket is not blocked even if queued data has not yet been sent or acknowledged. This is called a "hard" or "abortive" close, because the socket's virtual circuit is reset immediately, and any unsent data is lost. Any WSPRecv call on the remote side of the circuit will fail with WSAECONNRESET.

If SO_LINGER is set with a nonzero time-out interval on a blocking socket, the WSPClosesocket call blocks on a blocking socket until the remaining data has been sent or until the time-out expires. This is called a graceful disconnect. If the time-out expires before all data has been sent, the service provider should terminate the connection before WSPClosesocket returns.

Enabling SO_LINGER with a nonzero time-out interval on a nonblocking socket is not recommended. In this case, the call to WSPClosesocket will fail with an error of WSAEWOULDBLOCK if the close operation cannot be completed immediately. If WSPClosesocket fails with WSAEWOULDBLOCK the socket handle is still valid, and a disconnect is not initiated. The Windows Sockets SPI client must call WSPClosesocket again to close the socket, although WSPClosesocket can continue to fail unless the Windows Sockets SPI client disables SO_DONTLINGER, enables SO_LINGER with a zero time-out, or calls WSPShutdown to initiate closure.

If SO_DONTLINGER is set on a stream socket (that is, the l_onoff field of the linger structure is zero), the WSPClosesocket call will return immediately and does not get WSAEWOULDBLOCK, whether the socket is blocking or nonblocking. However, any data queued for transmission will be sent if possible before the underlying socket is closed. This is called a graceful disconnect and is the default behavior. Note that in this case the Windows Sockets provider is allowed to retain any resources associated with the socket until such time as the graceful disconnect has completed or the provider terminates the connection due to an inability to complete the operation in a provider-determined amount of time. This can affect Windows Sockets clients which expect to use all available sockets. This is the default behavior (SO_DONTLINGER is set by default)

Return Values

If no error occurs, WSPCloseSocket returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code is available in lpErrno.

Error Codes

WSAENETDOWN The network subsystem has failed.
WSAEINPROGRESS A blocking Windows Sockets call is in progress, or the service provider is still processing a callback function.
WSAENOTSOCK The descriptor is not a socket.
WSAEWOULDBLOCK The socket is marked as nonblocking and SO_LINGER is set to a nonzero timeout value.

QuickInfo

  Windows NT: Yes
  Windows: Yes
  Windows CE: Unsupported.
  Header: Declared in ws2spi.h.

See Also

WSPAccept, WSPSocket, WSPIoctl, WSPSetSockOpt