Graceful shutdown, linger options, and socket Closure

Source: Internet
Author: User

Client Side server side
(1) invokes shutdown (S, sd_send) to signal end of session and that client has no more data to send.
(2) es fd_close, indicating graceful shutdown in progress and that all data has been stored ed.
(3) sends any remaining response data.
(local timing significance only) gets fd_read and CILS Recv to get any response data sent by server. (4) invokes shutdown (S, sd_send) to indicate server has no more data to send.
(5) es fd_close indication. (local timing significance only) invokes closesocket .
(6) invokesClosesocket.

 

The following material is provided as clarification for the subject of shutting down socket connections closing the sockets. It is important to distinguish the difference 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, hereafter referred to as a shutdown sequence. two general classes of shutdown sequences are defined: graceful and abortive (also called hard ). 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 shutdown function, and the wsasenddisconnect function can be used to initiate a shutdown sequence, while the closesocket function is used to deallocate socket handles and free up any associated resources. some amount of confusion arises, however, from the fact that the closesocket function implicitly causes a shutdown sequence to occur if it has not already happened. in fact, it has become a rather common programming practice to rely on this feature and to use closesocket to both initiate the shutdown sequence and deallocate the socket handle.

To facilitate this usage, the sockets interface provides for controls by way of the socket option mechanic that allow the programmer to indicate whether the implicit shutdown sequence shocould be graceful or abortive, and also whetherClosesocketFunction shocould linger (that is not complete immediately) to allow time for a graceful shutdown sequence to complete. These important distinctions and the ramifications of usingClosesocketIn this manner are still not widely understood.

 

By establishing appropriate values for the socket options so_linger and so_dontlinger, the following types of behavior can be obtained withClosesocketFunction:

    • abortive shutdown sequence, immediate return from closesocket .
    • graceful shutdown, delaying 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 closesocket returns.
    • graceful shutdown, immediate return-allowing the shutdown sequence to complete in the background. although this is the default behavior, the application has no way of knowing when (or whether) the graceful shutdown sequence actually completes.

The use of the so_linger and so_dontlinger socket options and the associatedLingerStructure is discussed in more detail in the reference sections onsol_socket socket options andLingerStructure.

one technique that can be used to minimize the chance of problems occurring during connection teardown is to avoid relying on an implicit shutdown being initiated by closesocket . instead, use one of the two explicit shutdown functions, shutdown or wsasenddisconnect . this in turn causes an fd_close indication to be canceled ed by the peer application indicating that all pending data has been initialized ed. to initialize strate this, the following table shows the functions that wocould be invoked by the client and server components of an application, where the client is responsible for initiating a graceful shutdown.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.