Differences between socket shutdown and close Functions

Source: Internet
Author: User

If the server and client have established a connection, the server calls close and sends the fin segment to the client (in fact, it may not necessarily send the fin segment, which will be discussed later ), at this time, the server can no longer send and receive data through the socket. At this time, the client calls read. If the received fin segment returns 0, but the client can still write data to the server at this time, the write call is only responsible for handing over the data to the TCP sending buffer to return a successful result, so there is no error. The server replies to an rst segment after receiving the data, indicating that the server cannot receive the data and the connection is reset, after the client receives the RST segment, it cannot immediately notify the application layer. It only saves the status on the TCP protocol layer. If the client calls write again to send data to the server, the TCP protocol layer is already in the RST state, so the data is not sent, but a sigpipe signal is sent to the application layer, the default processing action of the sigpipe signal is to terminate the program.

 

Sometimes the code needs to call write multiple times in a row, and it may be too late to call read to find that the other party has closed the connection and the sigpipe signal is terminated, in this case, you need to call sigaction during initialization to process the sigpipe signal. We usually ignore the processing of this signal, signal (sigpipe, sig_ign ); if the sigpipe signal does not cause the process to exit abnormally (capture or ignore the signal), write returns-1 and errno is epipe (broken pipe ).

 

# Include <unistd. h>
Int close (int fd );

Close closes two data transmission directions.

 

# Include <sys/socket. h>
Int Shutdown (INT sockfd, int how );

Shutdown can choose to close either one direction or both directions. Shutdown how = 0 or how = 1 or how = 2 (shut_rd or shut_wr or shut_rdwr ), the latter two can ensure that the Peer receives an EOF character (that is, a fin segment is sent), regardless of whether other processes have enabled this socket. However, close cannot guarantee that only when the reference count of a sockfd is 0 will close send the fin segment. Otherwise, it only reduces the reference count by 1. That is to say, only when the socket is closed for all processes (several sub-processes may have opened this socket) will close send the fin segment.

Therefore, if shutdown how = 1 is called, it means that writing to a socket that has sent the fin is allowed. Receiving the fin segment only means that the other party no longer sends data, however, the other party can still read data, allowing the other party to continue reading the remaining data in the buffer.

 

========================================================== ========================================================== =

There are two ocket close and Shutdown

Differences between them:

Close ----- close the socket ID of the current process, but the link is still open. Other processes using this socket ID can use this link to read or write this socket ID.

Shutdown -- the socket link is broken. During reading, the EOF Terminator may be detected, and a sigpipe signal may be received during writing. This signal may remain

The socket buffer is filled before it is received. Shutdown also has a shutdown mode parameter. 0 cannot be read, 1 cannot be written, and 2 cannot be read or written.

========================================================== ========================================================== ====================================

Shutdown in multi-process socket, use close
After all data operations are completed, you can call the close () function to release the socket and stop any data operations on the socket:
Close (sockfd );


You can also call the shutdown () function to close the socket. This function allows you to stop data transmission in a certain direction.

Continue. For example, you can close the write operation of a socket and allow the socket to continue to accept data until all data is read.
Int Shutdown (INT sockfd, int how );
Sockfd is the descriptor of the socket to be closed. The how parameter allows you to select the following methods for the shutdown operation:
Shut_rd: Close the connected read end. That is, the socket no longer accepts data, and any data currently in the socket accept buffer will be discarded. The process will not be able

The socket sends out any read operations. Any data received after the call to the TCP socket will be confirmed and discarded silently.
Shut_wr: The write end that closes the connection. The process cannot write to this socket.
Shut_rdwr: It is equivalent to calling shutdown twice: first, shut_rd, and then shut_wr


Use close to abort a connection, but it only reduces the number of Descriptor references and does not close the connection directly. The connection is closed only when the descriptor reference number is 0.
Shutdown can directly close the descriptor, regardless of the reference number of the descriptor, you can choose to stop the connection in one direction.

Note:
1>. If multiple processes share one socket, close will be called once and the count will be reduced by 1 until the count is 0, that is, the process used calls close.

The link is released.
2>. In a multi-process, if one process is shutdown (SFD, shut_rdwr), other processes will not be able to communicate. If one process is close (SFD ),

 

 

A <---------------------------> B

1. Communication between A and B. After a sends a FIN packet to B, A cannot send data to B, but B can send data to a, and a must accept it.

2. Communication between A and B. After a calls close, if the socket reference count is 0, a will send a FIN packet to B, and a cannot send data to B, and a cannot accept data from B. At this time, B calls read. if it receives the fin segment, it will return 0, but B can still write to a at this time. The write call is only responsible for handing the data to the TCP sending buffer to return the result successfully, therefore, no error occurs. When a receives the data, it replies to an rst segment, indicating that a cannot receive the data.

A. B communication, a calls shutdown, regardless of the socket reference count. If it is shut_wr, A sends a FIN packet to B, indicating that a does not send data to B, but B can send data to a, and a can accept it.

If shut_wrrd is used, a sends fin to B, and a does not send data to B. If B sends data to a, B sends RST to a because the socket is closed.

If it is shut_rd, A does not read data. If B sends data to a, a sends RST to B.

Reprint a paragraph of http://www.haogongju.net/art/158060

 

  1. Difference between shutdown and close: (1) Calling shutdown will immediately close the specified link, and close will start to close the link when the reference counter of the descriptor is 0; (2) close closes two links at the same time, while the shutdown value closes the specified link. (3) after close, the file descriptor is no longer available (the reference base is 0, and resources are released ), after shutdown, the file descriptor is available. (page.172)

  2. After a shuteet descriptor shutdown shut_rd, according to unpv1, if the other party continues to send data, the data will still be confirmed by the receiver, but the receiver will automatically delete the data, will not be handed over to the user process. in Linux, if you want to send data through a shutdown shut_rd link, the sender will receive the rst. (page.173)

  3. For a socket descriptor shutdown shut_wr, the kernel will send fin after sending the cached data to start disconnecting (page.173)

  4. Shut_rdwr is equivalent to shutdown shut_rd first, and then shutdown shut_wr. (page.173)

Even if shut_rdwr is used, the shutdown (2) function does not release the file descriptor. Only the last close (2) Call releases the file descriptor. Otherwise, it remains available before that. Shutdown (2) can be called multiple times, as long as the socket remains in the connection status during this period.
Use close (2) to stop a connection. It only reduces the reference counter (references) of the descriptor and does not close the connection directly. The connection is closed only when the reference counter of the descriptor is 0 and the socket is released. For example, multiple processes share a socket. When close (2) is called once, the count is reduced by 1 until the count is 0, that is, the process used calls close (2 ), the socket is released.

 

Differences between socket shutdown and close Functions

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.