[Socket] shutdown and close in socket

Source: Internet
Author: User

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, while data transmission in one direction continues. 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 cannot perform any read operations on the socket. 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 is called once and the count is reduced by 1 until the count is 0, that is, close is called by all processes, and the socket is released.

2>. In a multi-process, if another process fails to communicate after Shutdown (SFD, shut_rdwr), close (SFD) does not affect other processes.

 

The socket uses close & shutdown to close the socket descriptor. There is a difference between the two.
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, close is the function to close the file descriptor in the system. Socket descriptor is also a file descriptor. Of course, it can also be closed using close. In general, close closes the read and write operations on the socket Descriptor and releases the system resources occupied by the descriptor. Any attempt to read or write the socket on the other end will return an error message .; However, when the descriptor is used by inter-process replication, several processes use the descriptor at the same time. At this time, a process calls close, which only reduces the number of references to this descriptor by one, the descriptor is disabled and released to occupy resources only when the number of references is reduced by one and then 0.

There are two restrictions on close:
(1) Close will close the reading and writing of the descriptor. This will easily lead to the close of the connection before reading all the data, so that the data sent by the other party will be discarded.
(2) for multiple process reference descriptors, close only removes the number of references to the descriptor by one until it is 0, therefore, when the referenced data is large and you want to disable the release descriptor, It is troublesome.

So the shutdown function is introduced, and shutdown -- destroys the socket link. During reading, the EOF Terminator may be detected, and a sigpipe signal may be received during writing, this signal may not be received until the socket buffer is filled. Shutdown has another shutdown mode parameter. 0 cannot be read, 1 cannot be written, and 2 cannot be read or written, this function can control the reading or writing of the socket. The socket receiving/transmission function is disabled. It allows users to disable communication in a certain direction or two-way communication. The prototype is as follows:
Int Shutdown (INT sockfd, int how); socket descriptor to be closed by sockfd.
How:
0-read prohibited
1-write prohibited
2-disabling read/write () is equivalent to close ()
When shutdown is set to 2, the number of references to the descriptor is not considered. In addition, read/write control can be implemented to make up for the lack of close.

If TCP is half-closed, if it is to be completely closed, send a FIN packet to the peer in both directions, and then respond to the peer's FIN packet (ACK, shutdown indicates that no data is sent, however, the FIN packet is not sent and the connection is not terminated. In fact, the TCP connection still exists. In this case, if you want to re-establish the socket pair (source/destination ports, the source/destination IP addresses are the same) if the connection still exists, you cannot create it again. If closesocket indicates that the connection has been completely terminated, you can use connect/accept to establish the connection again. Sending a termination command and disabling the client is the best way to avoid incomplete data transmission due to the server's rush to close the connection. shutdown is called the easy-to-close of a socket. in fact, it is not really closed. In the end, you need to call close to release the connection.

 

There are two close and shutdown ports for socket closure.

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 not be received until the socket buffer is filled, shutdown also has a shutdown mode parameter. 0 cannot be read, 1 cannot be written, and 2 cannot be read or written,

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.