Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

This function closes a socket.

Syntax

int WSPCloseSocket(
  SOCKET 
s,
  LPINT 
lpErrno 
);

Parameters

s

[in] Descriptor identifying a socket.

lpErrno

[out] Pointer to the error code.

Return Value

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

The following table shows the possible error codes.

Error value Description

WSAENETDOWN

Network subsystem has failed.

WSAEINPROGRESS

Blocking Windows Sockets call is in progress, or the service provider is still processing a callback function.

WSAENOTSOCK

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 sshould 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 WSPCloseSocketis called.

This function behaves in the following manner:

  • If SO_DONTLINGER is enabled (the default setting), this function returns immediately and connection is gracefully closed in the background.

  • If SO_LINGER is enabled with a zero time-out, this function returns immediately and the connection is reset/terminated.

    - or -

  • If SO_LINGER is enabled with a nonzero time-out with a blocking socket, this functionblocks until all data is sent or the time-out expires.

  • If SO_LINGER is enabled with a nonzero time-out with a nonblocking socket, this functionreturns immediately, thus indicating failure.

The following table shows how the semantics of this function are affected by the socket options SO_LINGER and SO_DONTLINGER.

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_onoffmember of the linger structure is nonzero) and the time-out interval, l_linger,is zero, this function 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 WSPRecvcall 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 WSPClosesocketcall 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 WSPClosesocketreturns.

Enabling SO_LINGER with a nonzero time-out interval on a nonblocking socket is not recommended. In this case, the call to WSPClosesocketwill fail with an error of WSAEWOULDBLOCK if the close operation cannot be completed immediately. If WSPClosesocketfails with WSAEWOULDBLOCK, the socket handle is still valid and a disconnect is not initiated.

The Windows Sockets SPI client mustcall WSPClosesocketagain to close the socket. However WSPClosesocketcan continue to fail. The following list shows the action the Windows Sockets SPI client must take to stop WSPClosesocketfrom failing:

  • Disables SO_DONTLINGER.

  • Enables SO_LINGER with a zero time-out.

  • Calls WSPShutdownto initiate closure.

If SO_DONTLINGER is set on a stream socket (that is, the l_onoffmember of the linger structure is zero), the WSPClosesocketcall 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 that expect to use all available sockets. This is the default behavior; SO_DONTLINGER is set by default.

Requirements

Header ws2spi.h
Library Ws2.lib
Windows Embedded CE Windows CE .NET 4.0 and later
Windows Mobile Windows Mobile Version 5.0 and later

See Also