Introduction and explanation of TCP status

Source: Internet
Author: User

In a twinkling of an eye, I have been engaged in server backend development for almost three years. Recently I want to summarize some more things. Let's start with the basics!

The company has gone through the Development and Restructuring of two frameworks, one of which is to convert the internal communication mechanism of the server from UDP to TCP.

First, let's talk about the basic applications of TCP, and the various statuses.

The following TCP status flowchart:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4C/47/wKioL1Q6ojLA0L_mAAL_U7tM3DM896.jpg "Title =" qq 511122320.6.png "alt =" wkiol1q6ojla0l_maal_u7tm3dm896.jpg "/> TCP connection, involving functions, mainly include:

Server: Socket bind listen accept read write close

Client: Socket connect write read close


Among them, the status changes mainly include:

Syn_sent syn_rcvd established fin_wait_1 fin_wait_2 close_wait time_wait last_ack

Close and other States.


TCP establishment and release, simply put, are three handshakes and four releases.

The three-way handshake process is as follows:

1. The server calls socket, bind, and listen to passively open the SDK and enter the listen status;

2. The client calls connect to open it and sends syn1 to the server to enter the syn_sent state;

3. The server confirms the SYN of the client and sends a syn2 and ACK to confirm the client and enter the syn_rcvd status;

4. The client receives the confirmation message sent by the server, enters the establish status, sends ack to the server, and the server receives ack and enters the establish status.

In this process, packets are often received when a TCP connection packet is captured through tcpdump.


The four release processes are as follows:

1. The party that proactively closes the service calls close and sends fin M, indicating that data is no longer sent and enters the fin_wait_1 state;

2. the passive closing party receives the fin M, enters the close_wait status, and sends the ACK m + 1;

3. The party that closes the service proactively receives ack m + 1 and enters the fin_wait_2 status;

3. Send fin n to the passive closing party and enter the last_ack status;

4. The party that closes the Service actively receives the fin N, enters the time_wait status, sends the ACK n + 1, and the party that closes the service passively receives

Ack, entering the closed status

In special cases, it is the party that closes the Service actively. It sends fin m and receives fin n instead of ack m + 1, indicating the other party.

It is also necessary to immediately close the data, so immediately transfer to the closed status.


Why do I need to shake hands four times when TCP is released?

If either party voluntarily disables the service, it can only ensure that the Party does not send any data, but it cannot determine whether to continue receiving the data;

Why do we need time_wait status?

1. Reliable termination of TCP full-duplex connections

Packet loss must be taken into account when network data is transmitted. If the last Ack is lost, the party that is passively disabled will resend the packet.

Its fin, so the party that proactively closes must maintain the status and allow itself to resend the final ack.

2. allow old duplicate segments to disappear in the Network

Time_wait: the maximum time for a network packet to survive.

If there is no time_wait status, client a sends data to server B and enters the closed status. After a while, I used the original

Port and IP address, and then re-establish the connection. In this case, the packets transmitted in the network may be received by the server after a period of time,

This is not what the server expects to receive.


Why is the server in the close_wait status?

When the server receives a request that the client closes, but does not close it, the server is in the close_wait status.

If the server does not receive the Recv packet, TCP replies to the ACK packet and enters close_wait.

Status.

If the vast majority of connections on the server are in the close_wait status, it is probably because the server itself has received and processed

Client data, caused by no close connection.


For more information, see

UNIX Network Programming volume 1

This article from "thinking precipitation" blog, please be sure to keep this source http://joezhengjinhong.blog.51cto.com/7791846/1563070

Introduction and explanation of TCP status

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.