Home  >  Article  >  php教程  >  Linux network programming--the difference between shut_down and close() functions

Linux network programming--the difference between shut_down and close() functions

高洛峰
高洛峰Original
2016-12-13 15:46:071904browse

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

1 #include

2 int close(intsockfd)

3 //Return: 0 - success, 1 - failure

4

5 #include

6 int shutdown(intsockfd,inthow to)

7 //Return: 0 - success, 1 - failure

The default action of calling close() on a tcp socket is to mark the socket as closed and immediately return to the process calling the api. At this time, from the perspective of the application layer, the socket fd can no longer be used by the process, that is, it can no longer be used as a parameter for read or write. From the perspective of the transport layer, TCP will try to send the data currently accumulated in the send buffer to the link, and then initiate TCP's 4 wave waves to completely close the TCP connection.
                                                                                                                                                   Calling close() is the normal way to close the TCP connection, but there are two limitations in this way, and this is the reason why shutdown() is introduced:
    1) close() actually just decrements the reference count of socket fd by 1, Only when the reference count of the socket fd is reduced to 0, the TCP transport layer will initiate a 4-way handshake to truly close the connection. Shutdown can directly initiate the 4 handshakes required to close the connection without being limited by the reference count;
2) close() will terminate the TCP duplex link. Due to the full-duplex nature of the TCP connection, there may be such an application scenario: the local peer will no longer send data to the remote peer, and the remote peer may still have data to send. In this case, if the local peer wants To notify the remote peer that it will no longer send data but will continue to receive data, close() cannot be used, but shutdown() can complete this task.


The first parameter of the close function and shutdown function both represents a file descriptor. We know that in the Linux operating system, everything is treated as a file, and all things, such as devices and memory, are simulated as files; of course, communication between networks is no exception. Each communication session has a file descriptor corresponding to it, and your operations between them are just like operating local files. In the shutdown function, there is also a parameter howto, which has the following three values ​​

SHUT_RD: close the reading half. At this time, the user can no longer read data from this socket, and the data received by this socket will be discarded. The peer is unaware of this process. Close the reading side of the connection. That is, the socket will no longer accept data, and any data currently in the socket's receive buffer will be discarded. The process will not be able to issue any read operations to the socket. Any data received after this call on the TCP socket will be acknowledged and then silently discarded.

SHUT_WR: Turn off the write half accordingly. At this time, the user can no longer write data to the socket. The kernel will send the data in the cache, and then no more data will be sent. The peer will know this. When the peer attempts to read, an error may occur.

SHUT_RDWR: Turn off the reading and writing halves. At this time, the user cannot read or write from the socket. It is equivalent to calling the shutdown function again, specifying SHUT_RD once and SHUT_WR once.

SHUT_** are defined by macros in the function library; because shutdown provides the second one, it can accurately control the closing of a socket descriptor, which is impossible for the close function. In a multi-threaded environment, a descriptor may be copied by several threads. They are associated with a file, and the kernel maintains a file reference count. Close can only close the file descriptor when the reference count is zero. .
There are two limitations in using the close function, but they can be avoided by using shutdown:

The close function decrements the reference count of the descriptor by one, and only when the count becomes 0, the socket is actually closed, and using shutdown The function can trigger TCP's normal connection termination sequence regardless of the reference count; the

close function terminates data transmission in both reading and writing directions. Since the TCP connection is full-duplex, sometimes we need to inform the peer that we have completed sending data. We only need to close a channel for data transmission, but we can still receive the data sent by the peer. This control is only This can be achieved using the shutdown function.

1>. If there are multiple processes sharing a socket, each time close is called, the count will be decremented by 1 until the count reaches 0, that is, all processes used have called close, and the socket will be released.

2>. In a multi-process, if one process shuts down (sfd, SHUT_RDWR), other processes will not be able to communicate. If a process closes (sfd), it will not affect other processes.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Linux command: shutdownNext article:Linux command: shutdown