[Zz] shutdown and close of Linux socket

Source: Internet
Author: User

Http://www.cublog.cn/u3/99348/showart_2072194.html

 


There are two methods for shutting down a connection in Linux socket: shutdown and close. First, let's take a look at the definition of shutdown.

 

# Include <sys/socket. h>

Int Shutdown (INT sockfd, int how );

There are three methods for how:

Shut_rd (0): Disable the READ function on sockfd. This option does not allow sockfd to perform read operations.

Shut_wr (1): Disable the FD write function. This option does not allow FD to write.

Shut_rdwr (2): Disable the FD read/write function.

If it succeeds, 0 is returned. if the error is-1, the error code errno: ebadf indicates that sockfd is not a valid descriptor; enotconn indicates that sockfd is not connected; enotsock indicates that sockfd is a file descriptor rather than a socket descriptor.

 

Close is defined as follows:

# Include <unistd. h>

Int close (int fd );

Disable read/write.

If the call succeeds, 0 is returned, and-1 is returned. The error code errno: ebadf indicates that FD is not a valid descriptor. eintr indicates that the close function is interrupted by signals. EIO indicates an IO error.

 

 

The following is an excerpt from the Internet to illustrate the differences between the two:

Close-------- Close the socket ID of this 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 remain

The socket buffer is filled before it is received. Shutdown also has a shutdown mode parameter. 0 cannot be read, 1 cannot be written, and 2 cannot be read or written.

Shutdown in multi-process socket, use close
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.

Continue. 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 will not be able

The socket sends out any read operations. 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 multi-process scenarios, if a shutdown (SFD, shut_rdwr) process fails to communicate with other processes. if a process is close (SFD), it will not affect other processes. you have to understand the reference count usage. better understanding of kernel programming knowledge.

 

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.