Differences between shut_down and close () functions in Network Programming

Source: Internet
Author: User
Tags terminates

In Linux C network programming, there are two methods to close a connected network communication: the close function and the shutdown function. Their function prototypes are:

1 #include<unistd.h>
2 int close(intsockfd)
3 // Return: 0 -- successful, 1 -- failed
5 #include<sys/socket.h>
6 int shutdown(intsockfd, int howto)
7 // Return: 0 -- successful, 1 -- failed

The default action to call close () for a TCP socket is to mark the socket as closed and immediately return it to the process of calling the API. In this case, the socket FD cannot be used by the process at the application layer, that is, it cannot be used as a parameter for read or write. From the perspective of the transport layer, TCP will try to send the backlog of data in the current send buffer to the link, and then initiate the TCP four waves to completely close the TCP connection.
Calling close () is a normal way to close the TCP connection, but there are two restrictions in this method, and this is exactlyReasons for introduction of Shutdown:
1) Close () actually only reduces the reference count of socket FD by 1. Only when the reference count of this socket FD is reduced to 0, the TCP transport layer will initiate four handshakes to actually close the connection. Shutdown can directly initiate the four handshakes required to close the connection, without being limited by the reference count;
2) Close () terminates the TCP duplex link. Due to the full duplex feature of the TCP connection, the local peer may not send data to the remote peer, and the remote peer may have data to send, in this case, if the local peer wants to notify the remote peer that it will not send data but will continue to receive data, close () will not work, and Shutdown () this task can be completed.

The first parameter of the close and shutdown functions represents a file descriptor. We know that in Linux, everything is treated as a file, and everything, such as devices and memory, is simulated into files. Of course, communication between networks is no exception. Each communication conversation corresponds to a file descriptor, and your operations between them are like operations on local files. In the shutdown function, there is another parameter howto, which has three values:

  1. Shut_rd: Close the read half. At this time, the user cannot read data from this socket, and the data received by this interface will be discarded. The peer does not know this process. 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.
  2. Shut_wr: Close the write half accordingly. At this time, the user cannot write data to the socket, and the kernel will send the cached data and will not send the data again, the peer will know this. An error may occur when the peer attempts to read data.
  3. Shut_rdwr: Close Reading and Writing. At this time, the user cannot read or write from the socket. It is equivalent to calling the shutdown function again, and specifying shut_rd at a time and shut_wr at a time.

Shut _ ** is defined by a macro in the function library. Because shutdown provides the second function, it can precisely control the shutdown of a socket descriptor, which cannot be implemented by the close function. In a multi-threaded environment, a descriptor may be copied by several threads and associated with a file, and the kernel maintains a file reference count, the file descriptor can be closed only when the reference count is zero.
There are two restrictions on using the close function, but shutdown can be used to avoid:

  1. The close function reduces the reference count of the descriptor by one. The socket is actually closed only when the count is changed to 0, the shutdown function can stimulate the normal connection termination sequence of TCP regardless of the reference count;
  2. The close function terminates data transmission in both read and write directions. Since the TCP connection is full-duplex, sometimes we need to inform the peer that we have completed data transmission, we only need to close a channel for data transmission, however, we can still receive the data sent from the peer end. This control can be achieved only by using the shutdown function.
  3. 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.
  4.    2>. In a multi-process, if another process fails to communicate after Shutdown (SFD, shut_rdwr), close (SFD) does not affect other processes.

Differences between shut_down and close () functions in Network Programming

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.