preface

Speaking of TCP, I believe everyone is familiar with, may think of the network seven-layer model, TCP/IP, UDP and so on. Transmission Control Protocol (TCP) is a connection-oriented, reliable (unlike UDP, which is unreliable), byte stream based transport layer (transport layer in the seven-layer model) communication Protocol. It supports timeout retransmission, flow Control, congestion Control, and other complex functions. That is, it is used for communication. So how does it communicate? Find out today.

The three stages of a TCP connection

Think about our daily communication scenario, for example, my roommate and I. We want to ‘write’ after work. There are three main steps. Step 2, [data transmission]- say blah blah blah blah; Step 3: disconnect — say good night or goodbye and close the door. This is the way we communicate, and it is the same as TCP, which is the transport layer communication. There are three stages:

  • Establishing a connection (TCP three-way handshake)

  • The data transfer

  • Disconnect (TCP wave four times)

Introduction to TCP datagrams

When an application layer program wants to send a packet to another application layer program, the transport layer adds a TCP/UDP header to the front of the packet according to the protocol (TCP/UDP) of the software, including the source port number, destination port number and other information. The transport layer sends the TCP/UDP header + packet to the network layer.

TCP header format:

  • Source port: indicates the service port of the sender
  • Destination port: indicates the service port of the receiver
  • Sequence Number (SEQ): Sequence Number set by the sender for the first byte of the data to ensure the Sequence of sending.
  • Acknowledgment Number (ACK Number): Indicates the sequence Number of the first byte of the data expected to be received in the next TCP packet segment from the sender. This field is valid only when the ACK flag bit is 1.
  • Flag bit: a total of 6 flag bits, with the value of 0/1, are URG, ACK, PSH, RST, SYN, and FIN.
    • ACK(Acknowledge) An acknowledgement flag, used to indicate that the acknowledgement number ACK is valid.

    • SYN(Synchronize) Synchronization flag, used only for TCP three-way handshake. This flag is valid and initializes a connection number.

    • FIN(Finish) Indicates that data is sent and the peer party is ready to disconnect. This flag is used for four TCP waves.

Note: Because of the above mentioned two ACKS, to prevent the expression of confusion, it is hereby stated that ACK indicates the confirmation flag, ACK indicates the confirmation number.

TCP three-way Handshake

Three handshakes

Description of handshake Process

  • First handshake

    User A sends A request for establishing A connection to user B. SYN=1 and SEQ= X are SENT. After the request is SENT, the user is in syn-sent state, waiting for the confirmation from the receiver.

  • Second handshake

    After receiving the request message, if USER B agrees to establish A connection (SYN=1), user B sends an ACK packet (ACK=1) to confirm the serial number of user A (ACK= X +1), and sends its own serial number SEQ= Y. After receiving the request message, user B is in syn-RCVD state.

  • Third handshake

    After receiving ACK+SYN from B, A confirms by sending ACK=1, ACK= Y +1, and SEQ= X +1. After sending, A is in ESTABLISHED state. When B receives an acknowledgement from A, The ESTABLISHED state is also entered.

Why three times?

Because TCP is a full-duplex and reliable communication protocol, in order to achieve reliable two-way communication, it needs three handshakes.

Because communication is a two-way (analogy for a two-way lanes, the lane, any party can be the sender, and can be the receiver), the sender and the receiver are need to confirm whether to send packets are received by the receiver, this through the serial number (SEQ) acknowledgment (ACK), when not received, Retransmission is required, and the easiest way to ensure mutual confirmation is to shake hands three times.

It can also be understood that the two parties of the communication need to make sure that the other party and themselves are reliable people, before they cooperate to do something important. The following figure shows that both parties need to confirm their own and the other party’s ability to send and receive messages. Only after confirming their own and the other party’s ability can they continue data transmission.

[picture archived failure outside the chain, the source station might be hotlinking prevention mechanism, proposed to directly upload picture preserved (img – lpgYkIgT – 1631716486842) (/ Users/zhangting/Desktop/TCP three-way handshake ability confirmed. PNG)]

What can go wrong with two handshakes?

The three-way handshake is mentioned in Computer Networks to prevent connection requests that have expired from being sent to the server and causing errors. Probably means that the sender of a connection is established the SYN requests for a long time due to network problems of stranded, lead to the request sender and receiver to the second communication connection to reach the receiver after his release, but this time the receiver thought is a new connection request from the sender, then sent a confirmation, thus established the connection is successful, but it is the recipient’s ideas, As for the sender, it has no intention of establishing a connection, so the receiver is just waiting for the communication from the sender. It is kind of like unrequited love, and the receiver will always consume his own resources.

If the SYN is invalid, a reset message is sent to the receiver. The third handshake is performed to avoid wasting resources and sending incorrect messages

The data transfer

This part will focus on the problems of data transmission and the corresponding solution mechanism, which will be put in the subsequent blog.

TCP waved four times

Four waves of the hand

The process of four waves can be seen as a couple firmly parting 💔 :

Love is dear, freedom is dear. To learn the four professional waves:

Description of waving process

  • First wave

    A sends A disconnection request to B: FIN=1, sends its serial number SEQ= U, stops sending data again, actively closes the TCP connection, and enters the FIN_WAIT1 state (terminates wait 1).

  • Second wave

    After receiving the message from A, B sends ACK=1 to confirm that it has received A’s serial number ACK= U +1 and sends its own serial number SEQ= V. Then B enters CLOSE_WAIT state.

    The TCP server notifies the upper-layer application process that A will not send any more data and B will not receive any data from A. The connection between A and B is closed and released.

    After receiving B’s message, A enters FIN_WAIT2(terminate wait 2) state, accepts the last data sent by B, and waits for B’s connection release message.

  • Third wave

    After sending the final data, USER B sends A connection release request: FIN=1, ACK=1,SEQ= W (more data may be sent), ACK= U +1 to user A and waits for the last ACK from user A.

  • Fourth wave

    After receiving the connection release request from USER B, user A sends an acknowledgement :ACK=1, ACK= V +1, SEQ= U +1, user A enters the TIME_WAIT state and waits 2MSL(Maximum Segment Lifetime). If B does not receive an ACK from B, it thinks that B has been disconnected and enters the CLOSED state, so it also disconnects and enters the CLOSED state.

    User B disconnects from user A and enters the CLOSED state after receiving the ACK packet from user A.

Three handshakes, why four waves?

TCP is a full-duplex communication protocol. Both parties must confirm that they have no information to send to each other before disconnecting. The first two waves are used to disconnect the AtoB direction, and the second two waves are used to disconnect the BtoA direction.

The handshake takes three times. During the second handshake, USER B sends A SYN+ACK to reply and synchronize. When waving, B does not immediately send the FIN because there may be some content to be sent. Instead, IT can only send ACK first, and then send the FIN after all the packets are sent, so it takes four times.

A wait 2MSL meaning

To ensure that the last ACK message of A can reach B, make B close the connection correctly and timely;

Within 2MSL, if the last ACK of A is lost, B will send FIN information again, AND A will send ACK again and wait for 2MSL. If A enters the CLOSED state immediately after entering the TIME_WAIT state, if the ACK is lost during transmission, B will never be CLOSED because A has been CLOSED and the FIN of B resending the ACK will not receive A response.

When A does not receive FIN again after waiting for 2MSL, it considers that ACK has been successfully received by B and can be relieved to disconnect.

Refer to the link

The Transformation of the Code Farmer

Blog.csdn.net/daocaoren15…

www.cnblogs.com/huigelaile/…

Blog.csdn.net/lengxiao199…

www.zhihu.com/question/24…

www.cnblogs.com/bj-mr-li/p/…

zhuanlan.zhihu.com/p/86426969

Mp.weixin.qq.com/s/D-29Y7UJq…

summary

This article has been written on and off for a long time. I hope you like ღ(´ ᴗ · ‘).