Socket close and Shutdown

Source: Internet
Author: User
From: http://hi.baidu.com/yoshubom/blog/item/caa3b2de5d210559cdbf1a2b.html


Socket close and Shutdown


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 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 will be called once and the count will be reduced by 1 until the count is 0, that is, the process used calls close.


The link is released.

2>. In a multi-process, if one process is shutdown (SFD, shut_rdwr), other processes will not be able to communicate. If one process is close (SFD ),


It affects other processes. You have to understand the usage of reference counting. You have a 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.