TCP shakes hands three times and waves four times

TCP header

The source port and destination port determine the processes of both sides at the TCP layer. The serial number represents the first byte number in the message segment data, and ACK represents the confirmation number. The sender of the confirmation number expects to receive the next serial number, that is, the last successfully received data byte serial number plus 1. This field is valid only when the ACK bit is enabled.

When a new connection is created, the syn bit of the first message segment sent from the client to the server is enabled, which is called syn message segment. At this time, the serial number field contains the first serial number to be used in this direction of the connection, that is, the initial serial number isn. The data sent later is isn plus 1, so the syn bit field will consume a serial number, This means reliable transmission using retransmission. An ACK that does not consume a serial number is not.

The header length (data offset in the figure) is in 32-bit words, that is, in 4 bytes. It has only 4 bits and the maximum is 15. Therefore, the maximum header length is 60 bytes and the minimum is 5, that is, the minimum header length is 20 bytes (the variable option is empty).

Legend of three handshakes and four waves

Three handshakes refer to sending three message segments, and four waves refer to sending four message segments. Note that both syn and fin segments will be reliably transmitted by retransmission.

The following figure is the sequence diagram of TCP three handshakes and four waves:

The following figure shows the TCP protocol state machine:

ACK - confirmation, so that the confirmation number is valid. Rst -- reset connection (often seen reset by peer) is the ghost of this field. Syn -- the serial number used to initialize a connection. Fin -- the sender of the message segment has finished sending data to the other party.

When a connection is established or terminated, the exchanged message segment only contains TCP header and no data.

Three handshakes

To figure out how many interactions TCP needs to establish a connection, we need to figure out what the goal of establishing a connection for initialization is.

The TCP handshake initializes a connection with the following objectives: allocating resources and initializing the serial number (notifying the peer of my initial serial number). Knowing the goal of initializing the connection, the process to achieve this goal is simple. The handshake process can be simplified to the following four interactions:

The initialization can be completed by 4 interactions in the whole process.

Why three handshakes

The client and server should be connected before communication. The function of "three handshakes" is that both parties can make it clear that their receiving and sending capabilities and those of the other party are normal.

First handshake: the client sends a network packet and the server receives it. In this way, the server can conclude that the sending ability of the client and the receiving ability of the server are normal.

The second handshake: the server sends the contract and the client receives it. In this way, the client can conclude that the receiving and sending capabilities of the server and the receiving and sending capabilities of the client are normal. From the perspective of the client, I received the response packet sent by the server, which indicates that the server received the network packet I sent during the first handshake and successfully sent the response packet, which indicates that the receiving and sending ability of the server is normal. On the other hand, I received the response packet from the server, indicating that the network packet I sent for the first time successfully arrived at the server. In this way, my own sending and receiving ability is also normal.

The third Handshake: the client sends the contract and the server receives it. In this way, the server can conclude that the receiving and sending ability of the client and the sending and receiving ability of the server are normal. After the first and second handshakes, the server does not know whether the receiving capacity of the client and its sending capacity are normal. In the third handshake, the server receives the response of the client to the second handshake. From the perspective of the server, the response data at the second handshake was sent out and received by the client. Therefore, my sending ability is normal. The receiving capacity of the client is also normal.

After the above three handshakes, both the client and the server confirm that their receiving and sending capabilities are normal. Then you can communicate normally.

Four waves

The goal of TCP disconnection is to reclaim resources and terminate data transmission.

Since TCP is full duplex, both ends of peer need to remove their own communication channels leading to the opposite end of peer. This requires four waves to remove the communication channel respectively, which is more clear.

At this point, wave four times and the TCP connection can be completely removed.

Why four waves

TCP connection is a peer-to-peer mode of two-way transmission, that is, both sides can send or receive data to each other at the same time.

When one party wants to close the connection, it will send instructions to inform the other party that I want to close the connection. At this time, the other party will return an ACK, and the connection in one direction will be closed. However, data can still be transmitted in the other direction. After all data is transmitted, a fin segment will be sent to close the connection in this direction. The receiver sends an ACK to confirm closing the connection.

Note that the party receiving the fin message can only reply to one ACK, which cannot immediately return to the other party's fin message segment, because the "instruction" to end data transmission is given by the upper application layer. I'm just a "Porter", and I can't understand the "will of the upper layer".

Why do you shake hands three times and wave four times

This is because when the server receives the syn message of the connection establishment request in the listen state, it sends the ACK and syn to the client in one message.

When the connection is closed, when the fin message of the other party is received, it only means that the other party no longer sends data but can still receive data. Whether our party closes the sending data channel now needs to be determined by the upper application. Therefore, our ACK and fin will generally be sent separately.

##################

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>