UNIX Network programming: The difference between shutdown and close functions

Source: Internet
Author: User
Tags socket connection reset

Assuming that the server and client have established a connection, the server calls close and sends a fin segment to the client (which does not necessarily send the fin segment, after that), when the server can no longer send and receive data through the socket. At this point, the client invokes read, and if the fin segment returns 0, but the client can write to the server at this time, the write call is responsible for sending the data to the TCP send buffer to successfully return, so there is no error, and the server received data to answer a RST segment, indicating that the server has been unable to receive data, connection reset, the client received a RST segment can not immediately notify the application layer, only this state is saved in the TCP protocol layer. If the client calls write data to the server again, because the TCP protocol layer is already in the RST state, the data is not emitted, but a sigpipe signal is sent to the application layer, and the default processing action of the sigpipe signal is the termination procedure.

Sometimes the code needs to call write several times in a row, it may not be too late to call read to know that the other side has closed the connection is sigpipe signal terminated, which requires the initialization of the call sigaction processing sigpipe signal, for this signal processing we usually ignore can, Signal (Sigpipe, sig_ign); If the sigpipe signal does not cause the process to exit abnormally, write returns-1 and errno is epipe.

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

Close closes the two directions of its own data transfer.

Close a TCP Socket's default behavior is to mark the socket as closed and immediately return to the calling process. The socket descriptor can no longer be used by the calling process, which means it cannot be the first parameter of read or write. However, TCP will attempt to send any data that has been queued to be sent to the right side, and a normal TCP connection termination sequence occurs after the send is completed.

We can use the SO_LINGER socket option to change this default behavior for a TCO socket.

The usual way to terminate a network connection is to call the close function. But close has two limitations, but you can use shutdown to avoid it.

(1) Close sets the reference count of the descriptor to 1 and closes the socket only when the count changes to 0 o'clock. Use shutdown to fire TCP's normal connection termination sequence regardless of reference count.

(2) Close terminates reading and writing data transfer in two directions. Now that the TCP connection is Full-duplex, sometimes we need to tell the right side that we have finished sending the data, even though there is still data to send to us on the side.

#include <sys/socket.h>  
int shutdown (int sockfd,int howto);  
      Return: If the success is 0, if the error is-1

The behavior of the function depends on the value of the HOWTO parameter:

Shut_rd close the read half of the connection, the socket does not have data to receive, and the existing data in the socket receive buffer is lost. The process can no longer call any read functions on such sockets. When the shutdown function is called on a TCP socket, any data received by the socket from the End-to-end is acknowledged and silently discarded.

SHUT_WR closes the connection to write this half, for TCP sockets, which is called semi shutdown. Data that is currently left in the socket send buffer is sent, followed by TCP's normal connection termination sequence. As we have said, the write half is closed, regardless of whether the reference count of the socket descriptor equals 0. The process can no longer invoke any write functions on such sockets.

Both the read and write halves of the Shut_rdwr connection are closed, which is equivalent to the call shutdown two times: the first call to the specified SHUT_RD, and the second call to the specified shut _WR.

Author: csdn Blog Ctthuangcheng

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/unix/

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.