Share via


Connection Shutdown

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

The following paragraphs describe operations incident to shutting down an established socket connection.

Initiating Shutdown Sequence

A socket connection can be taken down in one of several ways. WSPShutdown (with how equal to SD_SEND or SD_BOTH), and WSPSendDisconnect may be used to initiate a graceful connection shutdown. WSPCloseSocket can be used to initiate either a graceful or abortive shutdown, depending on the linger options invoked by the closing a socket. See the following sections for more information about graceful and abortive shutdowns, and linger options.

Indicating Remote Shutdown

Service providers indicate connection teardown that is initiated by the remote party through the FD_CLOSE network event. Graceful shutdown is also indicated through WSPRecv when the number of bytes read is 0 for byte-stream protocols, or through a return error code of WSAEDISCON for message-oriented protocols. In any case, a WSPRecv return error code of WSAECONNRESET indicates an abortive shutdown.

Graceful Shutdown, Linger Options, and Socket Closure in the SPI

It is important to distinguish between shutting down a socket connection and closing a socket. Shutting down a socket connection involves an exchange of protocol messages between the two endpoints, which is hereafter referred to as a shutdown sequence. Two general classes of shutdown sequences are defined: graceful and abortive. In a graceful shutdown sequence, any data that has been queued but not yet transmitted can be sent prior to the connection being closed. In an abortive shutdown, any unsent data is lost. The occurrence of a shutdown sequence (graceful or abortive) can also be used to provide an FD_CLOSE indication to the associated applications signifying that a shutdown is in progress. Closing a socket, on the other hand, causes the socket handle to become deallocated so that the application can no longer reference or use the socket in any manner.

In Windows Sockets, both the WSPShutdown function, and the WSPSendDisconnect function can be used to initiate a shutdown sequence, while the WSPCloseSocket function is used to deallocate socket handles and free up associated resources. However, the WSPCloseSocket function will implicitly cause a shutdown sequence to occur if it has not already happened. In fact, sometimes WSPCloseSocket is used to both initiate the shutdown sequence and deallocate the socket handle.

To facilitate this usage, the sockets interface provides for controls through the socket option mechanism that allows the programmer to indicate whether the implicit shutdown sequence should be graceful or abortive, and also whether the WSPCloseSocket function should linger that is, not complete immediately) to allow time for a graceful shutdown sequence to complete.

By establishing appropriate values for the socket options SO_LINGER and SO_DONTLINGER, the following types of behavior can be obtained with the WSPCloseSocket function.

  • Abortive shutdown sequence, immediate return from WSPCloseSocket.
  • Graceful shutdown, delay return until either shutdown sequence completes or a specified time interval elapses. If the time interval expires before the graceful shutdown sequence completes, an abortive shutdown sequence occurs and WSPCloseSocket returns.
  • Graceful shutdown, return immediately, and allow the shutdown sequence to complete in the background. This is the default behavior. Note, however, that the application has no way of knowing when (or whether) the graceful shutdown sequence completes.

One technique that can be used to minimize the chance of problems occurring during connection teardown is not to rely on an implicit shutdown being initiated by WSPCloseSocket. Instead, one of the two explicit shutdown functions (WSPShutdown or WSPSendDisconnect) are used. This in turn will cause an FD_CLOSE indication to be received by the peer application indicating that all pending data has been received. To illustrate this, the following table shows the functions that would be invoked by the client and server sides of an application, where the client is responsible for initiating a graceful shutdown.

Client side Server side

(1) Invokes WSPShutdown (s, SD_SEND) to signal end of session and that client has no more data to send.

(2) Receives FD_CLOSE, indicating graceful shutdown in progress and that all data has been received.

(3) Sends any remaining response data.

(5') Gets FD_READ and invoke recv to get any response data sent by server.

(4) Invokes WSPShutdown(s, SD_SEND) to indicate server has no more data to send.

(5) Receives FD_CLOSE indication.

(4') Invokes WSPCloseSocket

(6) Invokes WSPCloseSocket

The timing sequence is maintained from step (1) to step (6) between the client and the server, except for steps (4') and (5') which only have local timing significance in the sense that step (5) follows step (5') on the client side while step (4') follows step (4) on the server side, with no timing relationship with the remote party.

See Also

Concepts

Socket Connections on Connection-Oriented Protocols