preface

I wanted to implement a small function, and then I looked at the code of VLC, and then I found I couldn’t understand it, so I looked at the code of GCDAsynSocket, and then I went to learn the knowledge related to network, and recorded the understanding process of TCP protocol

This article is so good

TCP protocol format

To understand TCP, we need to look at the format of the protocol, that is, how the reliability of the protocol comes from. The diagram below.

message

Here are just a few to introduce, others can go to understand

The SYN flag bit

Set when a connection is requested. This flag bit is set during the TCP three-way handshake

ACK flag bit

The flag bit is set in the acknowledgement message

FIN flag bit

This flag bit is set when the connection is closed

Serial number (seq)

Indicates the serial number of the current packet.

The confirmation number (ack)

Is the sequence number of the next message that you want to obtain, which must be understood. For example, after A and B establish A connection, if A sends A message to B, the message is as follows: Seq =a, ACK = B indicates that the serial number of my current packet is A, and I want the serial number of the next packet you send me to be B. Then B’s reply to this packet is: SEq = B, ACK =a+1, which indicates that the serial number of my current packet is B, and I want the next packet you send me to be a+1. The next packet sent from A to B is seq: A +1, ACK = B +1, as shown in the following figure

The order and confirmation numbers of TCP packets are used to ensure the order of the packets and prevent them from being confused. The checksum is used to ensure that incomplete packets do not make errors. TCP also has a timeout retransmission mechanism to prevent packets from being lost. Order numbers ensure that data is not duplicated.

Wireshark View the FORMAT of TCP packets

The following describes the corresponding fields in detail, which is the same as the packet format. The following figure

TCP three-way handshake

1: The client sends a link request to the server. The packet is configured as follows: SYN is set to 1, indicating that it is a link request, and SEQ is arbitrary, I set it to K+1. The packet is as follows: SYN =1, seq=K.

2: The server receives the connection request from the client. An acknowledgement will be given for the request, and a SYN request will be sent, and seq will be set to J. The packet is as follows: SYN =1 ACK =1 SEQ=J, ACK = K +1(for confirming the packet K you sent me).

3: After receiving the packet from the server, the client sends an acknowledgement packet. The packet is ACK(flag bit)=1, ACK =J+1

TCP waved four times

Wireshark: SYN ACK seq ACK: SYN ACK seq ACK: SYN ACK seq ACK: SYN ACK Seq ACK: SYN ACK

Use the Wireshark to view details about TCP packets

Message 1

192.168. 0100.  -> 121.40172.169.The flag bit of the packet is ACK and SEQ=1095, ACK=26987
Next Sequence Number(NSEQ)=1095This is actually the SEQ of my next bag. The reply message of this packet should be ACK SEQ=26987,ACK=1095
Copy the code

Message 2

121.40172.169.    -->   192.168. 0100.
SEQ=26987(Is the ACK of the previous message, we said,ACK is the sequence number of the next little message I want you to send) ACK=1095(THE serial number of the next message I hope you send) NSEQ=28427(28427=26987+1440), this is the serial number of my next message. In fact, I can know the serial number of my next message, which is why the receiving end may accept the later message first. As long as my initial serial number is determined, the serial number of all the messages has been confirmed. The reply message of this packet should be ACK SEQ=1095 ACK=28427
Copy the code

Message 3

192.168. 0100.  -->  121.40172.169.
SEQ=1095(Number of the current packet) ACK=28427(Sequence number of the next message to be obtained) NSEQ=1095(Sequence number of its next packet) The reply packet of this packet should be SEQ=28427 ACK=1095
Copy the code

Message 4

121.40172.169.    -->   192.168. 0100.
SEQ=28427
ACK=1095
NSEQ=29867
Copy the code

At this point, the process of TCP packet transmission is analyzed. The whole process is plotted as follows.

Interesting explanation of TCP sliding window

Teacher: Have a good rest at home this week and don't assign any homework. Student: This week everyone..... Scene 2 Teacher: This student: This teacher: Zhou Student: Zhou Teacher: So slow I want to get off work earlier scene 3 Teacher: This week everyone in student: this week everyone in teacher: home have a good rest student: home good....... Teacher: Everyone is in this week Student: Everyone is in this week Teacher: Have a good rest at home Student: Have a good rest at home The teacher outside noisy, this hair two teachers: home good student: home good.. The teacher is not so noisy, this time sent three teachers: have a good rest students: have a good rest... No noise, 10 teacher: no homework student: no homeworkCopy the code

conclusion

The above is my personal understanding of TCP protocol, if there is any error welcome to correct.