Expand Minimize

WSPCloseSocket function

The WSPCloseSocket function closes a socket.

Syntax


int WSPCloseSocket(
  _In_   SOCKET s,
  _Out_  LPINT lpErrno
);

Parameters

s [in]

Descriptor identifying a socket.

lpErrno [out]

Pointer to the error code.

Return value

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 codeMeaning
WSAENETDOWN

The network subsystem has failed.

WSAEINPROGRESS

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

Socket is marked as nonblocking and SO_LINGER is set to a nonzero time-out value.

 

Remarks

This function closes a socket. More precisely, it releases the socket descriptor s, so 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:

  • If SO_DONTLINGER is enabled (the default setting), WSPCloseSocket returns immediately and connection is gracefully closed in the background.
  • If SO_LINGER is enabled with a zero time-out, WSPCloseSocket returns immediately and the connection is reset/terminated.

    or

  • If SO_LINGER is enabled with a nonzero time-out with a blocking socket, WSPCloseSocket blocks until all data is sent or the time-out expires.
  • If SO_LINGER is enabled with a nonzero time-out with a nonblocking socket, WSPCloseSocket returns immediately, thus indicating failure.

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

OptionIntervalType of closeWait for close?
SO_DONTLINGERDo not careGracefulNo
SO_LINGERZeroHardNo
SO_LINGERNonzeroGracefulYes

 

If SO_LINGER is set (that is, the l_onoff member 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 Winsock SPI client must call WSPCloseSocket again to close the socket, although WSPCloseSocket can continue to fail unless the Winsock SPI client does one of the following:

  • Disables SO_DONTLINGER.
  • Enables SO_LINGER with a zero time-out.
  • Calls WSPShutdown to initiate closure.

If SO_DONTLINGER is set on a stream socket (that is, the l_onoff member 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 Winsock 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 Winsock clients that expect to use all available sockets. This is the default behavior; SO_DONTLINGER is set by default.

Requirements

Minimum supported client

Windows 2000 Professional [desktop apps only]

Minimum supported server

Windows 2000 Server [desktop apps only]

Header

Ws2spi.h

See also

WSPAccept
WSPIoctl
WSPSetSockOpt
WSPSocket

 

 

Community Additions

ADD
Show:
© 2015 Microsoft