Several statuses of TCP connection termination

Source: Internet
Author: User

TCP termination requires four sub-segments, or four handshakes.

 

The first clear concept is that when a TCP sockets connection is established, the customer and the server are equal and there is no difference;

TCP connections are duplex, so a connection is actually "two one-way connections"

 

Connection termination:

1. The active Party sends a message with a fin bit, indicating that the one-way connection from the local device to the remote device is terminated;

At this time, the active party enters the fin_wait1 state, which means it is waiting for the remote fin message;

 

2. The remote side will immediately send the ACK after receiving the fin; the active side receives the ACK and enters the fin wait2 status, so the FIN-WAIT1 status continues very short;

At this time, the remote end enters the close-Wait state, and one unidirectional connection is terminated, but the other is not, and is in the half-close connection state;

 

3. When the remote end sends necessary data, it sends fin, indicating that the one-way connection from it should also be closed; at the same time, it enters the last ack state;

 

4. After receiving the fin, the active Party responds to an ACK. The remote end enters the closed status and the connection is closed;

 

5. the active party enters the time Wait Status; ensures that the last Ack is not lost, and prevents the new connection from occupying the address port of the active party just closed, so that the group of old and wandering connections in the network is mistaken for the group of new connections.

 

Concept of population:

Semi-open connection: one party has closed or terminated the connection abnormally, but the other party does not know. For example, if an end suddenly loses power, the other end cannot perceive it.

1. As long as there is still unread (read) data in the TCP buffer, the RST will be sent directly to the peer end when close is called.
2. Shutdown has nothing to do with the socket descriptor. Even if Shutdown (FD, shut_rdwr) is called, The FD will not be closed. In the end, close (FD) is required ).
3. Write this socket descriptor after the fin package has been sent will cause epipe/sigpipe.

4. When multiple socket descriptors direct to the same socket object, when you call close, the reference count of the object will be decreased first. If the count is 0, the FIN packet will be sent to end the TCP connection. If shutdown is different, the fin package is sent if it is called in shut_wr/shut_rdwr mode.
5. so_linger and close. When the so_linger option is enabled but the timeout value is 0, call close to directly send the RST (this can avoid entering the time_wait status, however, the normal working mode of the TCP protocol is damaged. so_linger has no effect on shutdown.
6. There is no direct relationship between the RST and the possible time_wait status in the TCP connection. The active fin package will inevitably enter the time_wait status, unless the fin is not sent and the RST is sent directly to end the connection.

 

References:

UNIX Network Programming volume 1;

First volume of Internet connection using TCP

A useful tcpdump command is provided:

Tcpdump-S0-Xn-I any UDP and IP addresses [0x29: 4] = 831010004

The packet that captures the UDP packet and starts from bytes 0x29 with a value of 831010004 for 4 consecutive bytes;

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.