Difference between close and Shutdown

Source: Internet
Author: User

Transferred, not verified

Close (sock_fd) will reduce the internal counter of sock_fd by 1
When the internal counter of sock_fd is 0, shutodwn () is called and the file descriptor is finally released.
When Shutdown () is called, TCP disconnection is only performed and the file descriptor is not released.

Normally, the TCP program does not need to display the call Shutdown ()
However, some TCP programs are unfriendly, including the well-known early versions of Firefox.
When shutodwn () is called, no close_wait will be called. Only fin_wait1 or fin_wait2 will be called.

 

This is because the server does not call shutdown.
Close_wait will be brought to the server when the client exits abnormally.

Close_wait is very annoying and will get stuck (BLOCKED) Close () function

 

 

The following describes TCP sockets on Windows.

First, you need to distinguish between closing the socket and closing the TCP connection. Closing the TCP connection refers to the thing at the TCP protocol layer, that is, the two TCP ends exchange some protocol packets (FIN, RST, etc.), the specific exchange process can be viewed in the TCP protocol, which is not described here. To close a socket is to close the socket handle in your application and release related resources. However, when you close the socket handle, the TCP connection is closed implicitly.

There are two methods to close a TCP connection: graceful close and hard close or abortive close ). The so-called elegant shutdown refers to the process of sending a fin package after the data in the sending cache is sent out and the ACK of all data is received. Force disable means that if there is still data in the cache, the data will be discarded, and then the RST packet will be sent to reset the TCP connection.

 

The shutdown and closesocket functions are described below.

The shutdown function is prototype:

Int Shutdown (

Socket s,

Int how

);

This function is used to close a TCP connection. A single socket handle is not closed. The second parameter can have three values: sd_receive, sd_send, and sd_both.

Sd_receive indicates that the receiving channel is disabled and data cannot be received on the socket. If data is still not retrieved from the received cache or is received later, TCP sends an RST packet to the sender and resets the connection.

Sd_send indicates that the sending channel is disabled. TCP will send the data in the sending cache to the peer end after receiving the ACK of all data, indicating that no more data is sent at the local end. This is an elegant closing process.

Sd_both indicates that both the receiving and sending channels are disabled.

 

From: http://blog.csdn.net/bad_sheep/article/details/6157738

 

 

The closesocket function is prototype:

Int closesocket (

Socket s

);

This function is used to close the socket handle and release related resources. As mentioned above, closing the socket handle will implicitly trigger the closing process of the TCP connection. Is closesocket triggering an elegant closing process or a forced closing process? This is related to a socket option: so_linger option. The setting value of this option determines the behavior of closesocket. The parameter value of this option is in the linger structure and is defined:

Typedef struct linger {

U_short l_onoff;

U_short l_linger;

} Linger;

When the l_onoff value is set to 0, closesocket will return immediately and close the user's socket handle. If no data is sent in the buffer, the system will close the TCP connection after the data is sent in the background, however, one side effect is that the underlying resources of the socket will be retained until the TCP connection is closed, which cannot be controlled by the user application.

When the Rochelle off value is set to a non-zero value and the Rochelle linger value is set to 0, closesocket immediately returns and closes the user socket handle. However, if no data is sent in the buffer, TCP will send an RST packet to reset the connection, and all unsent data will be lost. This is a forced shutdown process.

When the Rochelle off value is set to a non-zero value and the Rochelle linger value is set to a non-zero value, if the socket is blocked, if no data is sent in the buffer zone, if TCP sends all data within the time indicated by Rochelle linger, the TCP connection is closed after the data is sent; if TCP does not send all data within the time indicated by l_linger, it will discard all unsent data and then send the RST packet to reset the connection. This is a forced shutdown process.

There is also a socket option so_dontlinger whose parameter value is of the bool type. If it is set to true, it is equivalent to setting Rochelle Onoff to 0 in so_linger.

Note that the so_linger and so_dontlinger options only affect closesocket behavior, but they are irrelevant to the shutdown function. Shutdown always returns immediately.

 

Therefore, to ensure that the recommended best way to close is as follows:

After all data is sent:

(1) Call Shutdown (S, sd_send). If the local end also receives data, perform Step 2; otherwise, skip to step 2.

(2) continue to receive data,

(3) After receiving the fd_close event, call the Recv function until the Recv returns 0 or-1 (ensure that all data is received ),

(4) Call closesocket to close the socket handle.

 

In actual programming, we often do not call shutdown, but directly call closesocket, using closesocket to implicitly trigger the characteristics of the TCP connection closing process. The process is as follows:

After all data is sent:

(1) If the local end also accepts data, perform Step 2; otherwise, skip to step 2.

(2) continue to receive data,

(3) After receiving the fd_close event, call the Recv function until the Recv returns 0 or-1 (ensure that all data is received ),

(4) Call closesocket to close the socket handle.

However, to ensure that data is not lost, you need to set the so_dontlinger option. However, this option is also the default setting on Windows.

 

After experiments, it is found that even if the sending application unexpectedly exits or is killed, the operating system will not discard the unsent data in the sending buffer, but will send the data in the background. However, this is because the sending cache of the socket is not 0. It is special when the sending cache of the socket is set to 0 (through the so_sndbuf option). Whether the socket is blocked or not, the send function will be blocked until the data in the passed-in user cache is sent and confirmed, because at this time, the driver layer does not allocate a cache to store user data, instead, it directly uses the user cache at the application layer. Therefore, it must be blocked until all data is sent. Otherwise, the system may crash.

 

In addition, if the application at the receiving end unexpectedly exits or is killed, and there is still data in the receiving cache, the TCP at the receiving end will send an RST packet to the sending end, reset the connection because subsequent data cannot be submitted to the application layer.

 

Finally, we will talk about a bug in windows, that is, a test like this:

Connect to the other end and connect to the other end. After the connect operation is successful, the listen end detects a network event trigger and kill the connect end before the accept operation on the listen end, then, continue to run the listen client. However, the listen client will still succeed in accept, and the data can be successfully sent from the socket that comes out of accept. After a network event is sent, the system waits for success again, but the event ID obtained by calling wsaenumnetworkevents is 0. After that, you will never wait for network events.

Difference between close and 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.