WSPCloseSocket (Windows CE 5.0)
This function closes a socket.
int WSPCloseSocket(SOCKET s,LPINT lpErrno );
Parameters
- s
[in] Descriptor identifying a socket. - lpErrno
[out] Pointer to the error code.
Return Values
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 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.
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 function 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, this function****returns 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_onoff member 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 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. However WSPClosesocket can continue to fail. The following list shows the action the Windows Sockets SPI client must take to stop WSPClosesocket from failing:
- 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 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
OS Versions: Windows CE .NET 4.0 and later.
Header: Ws2spi.h.
Link Library: Ws2.lib.
See Also
WSPAccept | WSPSocket | WSPIoctl | WSPSetSockOpt
Send Feedback on this topic to the authors