Basic concepts in Linux network programming

Source: Internet
Author: User

int close (int fd) (assuming server side)

Close closes the two directions of its own data transfer. The default behavior of the close one TCP socket is to mark the socket as closed and then immediately return to the calling process. The socket descriptor can no longer be used by the calling process, which means that it cannot be the first parameter of read or write. However, TCP will attempt to send any data that has been queued for delivery to the peer, and a normal TCP connection termination sequence occurs after the send is complete.

We can use the SO_LINGER socket option to change this default behavior for TCP sockets.

Note: Close will first attempt to send data in the socket send buffer of this socket and then perform TCP release operations, (normally 4 sections released), but close by default, the default return of close only succeeds in issuing a fin subsection, Also does not mean that the peer has been confirmed.

If close, the socket's receive buffer has data that is not read by the application layer, and closing the socket causes the RST to be sent directly to the peer (after close, this end actively transmits RST), http://my.oschina.net/costaxu/ The third case of blog/127394

After the server-side close, the reference count of the socket descriptor is reduced by 1, and if it is 0 o'clock (the socket is a file), the socket is closed. At this point, call Read or write (FD ...) on the server side. A ERRNO=EBADF/* Bad file Descriptor */error will appear.

Shutdown (FD, SHUT_RD) told the kernel that I was on a hunger strike and had to eat (data) and not put it on my table (receive buffer) and throw it directly to the dog. (But it can still be sent to the receive buffer, just be discarded by TCP) shutdown (FD, SHUT_WR) The core sends fin to the other side of the data sent to know shut_rd not send fin,shut_rw more active emit fin

The role of the shutdown function

Shutdown:SHUT_RD:read has no effect, just returns 0;

Close the connection read this half, the process can no longer invoke any read operation on such a socket;

The receive request cannot be made on the socket, the process can still send data to the socket, all data in the socket receive buffer is discarded, any data received by the socket is discarded by TCP and has no effect on the socket send buffer;

SHUTDOWN:SHUT_WR: However, the Write method cannot be called after the call shutdown close write, which can cause an error to terminate the program, which differs from the read above, in fact, after the FIN packet has been sent, write the socket descriptor will be raised epipe/ Sigpipe.

Close the connection to write this half, the process can no longer invoke any write operation on such a socket;

The send request cannot be sent on the socket, the process can still receive data from the socket, the contents of the socket send buffer are sent to the peer, followed by the normal TCP connection termination sequence (that is, send fin), no effect on the socket receive buffer;

After the server SHUTDOWN:SHUT_RD, then this port read (FD) returns 0, stating the end of the file, http://blog.chinaunix.net/uid-23629988-id-3035613.html

The Linux version is kernel-2.6.21:

1, as long as the read buffer in the TCP stack has unread (read) data, the call to close will send the RST directly to the peer.

2,shutdown does not have a relationship with the socket descriptor, even if the call to shutdown (FD, SHUT_RDWR) does not close the FD, and eventually close (FD) is required.

3, it can be considered that shutdown (FD, SHUT_RD) is an empty operation, because after shutdown can continue to read data from the socket, this may need further confirmation.

4, write the socket descriptor after the fin package has been sent raises epipe/sigpipe.

5, when more than one socket descriptor points to the same socket object, calling close first decrements the object's reference count, and the count is 0 o'clock to send the FIN packet end TCP connection. Shutdown, a FIN packet is sent as long as it is called in Shut_wr/shut_rdwr mode.

6,so_linger and Close, when the So_linger option is on but the timeout value is 0 o'clock, call close to send the RST directly (this avoids entering the time_wait state, but destroys the way the TCP protocol works), So_ Linger has no effect on shutdown.

7,TCP connection on the emergence of the RST with the subsequent possible time_wait state is not directly related to the active fin outsourcing will inevitably enter the TIME_WAIT state, unless the fin is sent directly to send RST end connection.

Basic concepts in Linux 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.