The close () and shutdown () functions

Source: Internet
Author: User
Tags connection reset

One close (int sockfd)

When the server and client establish a connection, the server calls close (), and the server sends fin to Client,server not to continue transmitting the message through the socket or to receive the message, at which point the client calls read and returns 0 if the fin is received. However, the client can still write to the server at this time, and write simply sends the information to the buffer, and when the server receives the datagram it returns an RST datagram connection reset, indicating that the server no longer accepts the message and requires a connection reset if it is to deliver the message. When the client receives the RST, it is not transmitted directly to the application layer, but the state is saved in the TCP protocol layer, and when the client calls write to the server again, the data is no longer emitted because the connection is already in the RST state. Instead, a signal is sigpipe to the application layer. Only this count is 0 o'clock closes the socket, sending the normal connection termination sequence of TCP

And the function is not a true four-stop sequence, but rather a descriptor's access count of 1,

Two shutdown (int sockfd,int howto)

1>close the access count of the descriptor-1, only the socket is closed at this count of 0 o'clock. The normal connection termination sequence of TCP is fired with shutdown, regardless of the access count.

2>close terminated the two directions of data transfer: Read and write. Because the number of TCP connections is full, there are a lot of times to tell the other side that the data is being sent, even if the other end has a lot of data to send, the shutdown function can just close the connection read, write, or two directions off

Howto

1.SHUT_RD closes the read half of the connection, no longer accepts data from the socket, and the data left in the socket receive buffer is invalidated, and the process can no longer perform any read processes on the socket. After this function is called, the data received by the TCP socket interface is received, but the data itself is discarded.

2.SHUT_WD close the write half of the connection this condition is called half-closed, and the data currently left in the socket is sent to the buffer, followed by the normal TCP termination sequence, and the process cannot perform any write functions on the socket interface.

Read this half of the 3.shut_rdwr connection and write this half off, equivalent to calling the function shutdown () two times, the first call with Shut_rd, and the second call with SHUT_WR.

  

The close () and shutdown () 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.