当前位置:网站首页>Explain the establishment of TCP connection in detail
Explain the establishment of TCP connection in detail
2022-07-23 12:42:00 【Maybe I'm dissolute】
TCP Format of the first part
TCP In front of the header 20 The bytes are fixed , In the back 4N Bytes are options added as needed , therefore TCP The minimum length of message segment is 20 byte .
The meaning of the fields in the first fixed part is as follows :
1. Source port and destination port : add IP The source of the first part IP Address and purpose IP Address , Determine the only one TCP Connect . In addition, it is determined by the destination port TCP Which application will the datagram be delivered to , So as to achieve TCP Distribution function of .
2. Serial number : Occupy 4 Bytes , The range of serial numbers is [0 To 2 Of 32 The next power ], because TCP Is oriented to a byte stream , In a TCP Each byte in the byte stream transferred in a connection is numbered in order , The serial number field in the header refers to the serial number of the first byte of the data sent by this message segment , In addition, the serial number is recycled , When the serial number increases to the maximum , The next serial number goes back to 0.
3. Confirmation no. : When ACK Sign bit is 1 Effective when , Represents the sequence number of the first data byte of the next message segment expected to be received . The confirmation number is N, Means serial number N-1 All data bytes so far have been received correctly .
4. The length of the first :TCP The first length of a message segment , It points out that TCP The starting position of the data part of the message segment is the same as TCP The distance from the starting position of the message segment . The length of the head accounts for 4 Bytes , But its unit is 4 byte , Therefore, the maximum length of the head is 15×4=60 byte , This means that the length of the options does not exceed 40 byte .
5. Keep a : It has to be for 0.
6. Below 6 A control bit indicates the nature of the message segment :
(1)URG(URGent): Used in conjunction with the emergency pointer field in the header . When URG by 1 when , Indicates that the emergency pointer field is valid , Send what the application process tells the sender TCP There's emergency data to transmit , So the sender TCP Insert the emergency data at the beginning of the paragraph , And behind it is still ordinary data .
(2)ACK(ACKnowledgement): Only when the ACK=1 The validation number field is valid when , When ACK=0 when , Invalid confirmation number .TCP Regulations : After the connection is established, all transmission message segments are set 1.
(3)PSH(PUSH): If the sent message segment PSH by 1, After receiving the message segment , Deliver it directly to the application , Instead of waiting for the cache to fill up before interacting up .
(4)RST(RESET): Reset flag ,RST by 1 when , indicate TCP There was a serious error in the connection , Connection must be released , Then reestablish the transport connection .
(5)SYN(SYNchronization): Synchronization sequence number , Used to initiate a connection . When SYN=1 and ACK=0 when , Indicates that this is a connection request segment , If the other party agrees to establish a connection , In the response message segment SYN=1 and ACK=1.
(6)FIN(FINish): Used to release a connection . When FIN=1 when , It indicates that the data of the sender of this message segment has been sent , And ask to release the connection .
7. window : The sending window size set by the receiver when the sender sends the message segment next time .
8. The checksum : The field range of verification includes header and data .
9. Pointer to an emergency : When URG=1 Only when effective , It indicates the number of bytes of urgent data in the paragraph .PS: Even if the window is 0 when , Emergency data can also be sent .
10. Options and fill : The option should be 4 Integer multiples of bytes , Otherwise, use 0 fill . The most common optional field is the longest message size MSS(Maximum Segment Size), Each party usually indicates this option in the first segment of the communication . It indicates the maximum length of message segment that can be received by the local end . If this option is not set , The default is 536(20+20+536=576 Of IP The datagram ), among IP The first and TCP Head each 20 Bytes , and internet Standard MTU(Maximum Transmission Unit Maximum transmission unit ) The minimum is 576B.TCP Connection establishment
Server side TCP The process first creates the transport control block TCB, Ready to receive the connection request from the client process , Then the server process is in LISTEN, Wait for the client's connection request , if there be , Then respond .
1. Client's TCP The process also first creates the transmission control module TCB(Transmission control module), Then send the connection request message segment to the server , In the header of this message segment SYN=1,ACK=0, At the same time, select an initial sequence number seq=i.TCP Regulations :SYN=1 The message segment of cannot carry data , But to consume a sequence number . At this time ,TCP The client process enters SYN-SENT( Sync sent ) state , At this time TCP My first handshake .
2. After the server receives the request message from the client , If you agree to establish a connection , Send a confirmation to the client . Confirm... In the message SYN=1, Confirmation no. ack=i+1, At the same time, choose an initial serial number for yourself seq=j. This message segment is also SYN=1 Message segment , Can't carry data , But also consume a sequence number . At this time ,TCP Server access SYN-RCVD(received), This is a TCP The second handshake of the connection .
3.TCP After the client process receives the confirmation from the server process , Also give confirmation to the server . Confirm the ACK=1, Confirmation no. ack=j+1, And my serial number is seq=i+1.TCP Standard provisions of ,ACK The message segment can carry data , But if it does not carry data, it does not consume the serial number . therefore , If you don't carry data , Then the sequence number of the next message segment is still seq=i+1. At this time ,TCP Connection established , Client access ESTABLISHED( Connection established ) state . This is a TCP The third handshake of connection , It can be seen that the client of the third handshake can send the message segment with data .
When the server receives the confirmation , Also enter the established connection state .Both parties actively connect at the same time TCP Connection establishment process
Under normal circumstances , Transmission connections are initiated by one party , However, it is also possible for both parties to initiate a connection at the same time , At this time, connection collision will occur , Finally, only one connection can be established . Because all connections are identified by their endpoints . If the first connection request is established by a socket (x,y) Connection of identification , And the second connection also establishes such a connection , So in TCP There is only one socket entry inside the entity .
When a connection request is made at the same time , Then both ends send one at almost the same time SYN Field settings 1 Data section of , And enter SYN_SENT state . When each end receives SYN Data segments , The status changes to SYN_RCVD, At the same time, they are sent SYN Field settings 1,ACK Field settings 1 Data section of , Yes, I received SYN Data segment for confirmation . When both parties receive each other's SYN+ACK After the data segment , They all enter ESTABLISHED state . But ultimately, a TCP Connect .PS: A transmission connection opened by both parties at the same time needs to be exchanged 4 Segment data segment , Than normal transmission connection establishment 3 One handshake and many exchanges 1 Data segments . At this time, neither end is called a client or server , Because each end is both a client and a server .
Why do you have to shake hands three times ?
1. front 2 A handshake is obviously necessary , Mainly for the last time , This is mainly to prevent the connection misjudgment caused by the failed request message segment suddenly being sent to the server .
2. Consider the following : The client sends a connection request message segment to the server , But on some network nodes for a long time , Then the client sends a connection request message segment to the server over time , Then establish the connection normally , Data transmission completed , And released the connection . If the first request sent at this time is delayed for a period of time , It's the server side again , Obviously , This is a message segment that has already failed , But when the server receives it, it mistakenly thinks that the client has sent another connection request , Then send the confirmation message segment to the client , And agreed to establish a connection . Suppose you don't use 3 The second handshake , At this time, as long as the confirmation is sent , A new connection is established , But because the client did not send a request to establish a connection , Therefore, we will not pay attention to the confirmation of the server , It will not send data to the server , But the server thinks the new connection has been established , And has been waiting for the client to send data , In this way, the server will always wait , Until the set value of the activation counter is exceeded , And the client is judged as having a problem , To close this connection , This wastes a lot of server resources . And if you use three handshakes , The client will not send confirmation to the server , The server cannot receive the confirmation , Just know that the client doesn't ask for a connection , Thus no connection is established .
边栏推荐
猜你喜欢

HCIP---BGP相关配置

Implementation of binary tree -c
![[fee of AUTOSAR (difference between nonvolatile memory flash and EEPROM)]](/img/cc/34bfcc450d82befab24173b0cb132d.png)
[fee of AUTOSAR (difference between nonvolatile memory flash and EEPROM)]

Unity3d:场景加载 GameObejct上脚本执行顺序

unity3d:Assetbundle模拟加载,同步加载,异步加载,依赖包加载,自动标签,AB浏览器,增量打包
![[memory understand the difference and function between ram flash and EEPROM memory]](/img/71/c9ada89e0a876e0581a7b5ceec2bff.png)
[memory understand the difference and function between ram flash and EEPROM memory]

LVS负载均衡调度原理及配置方法

Prometheus Operator使用指南笔记

Basic OJ exercise of binary tree-

GameFramework:打包资源,打随app发布包,打包生成文件夹说明,上传资源至服务器,下载资源,GameFreamworkList.dat 与GameFrameworkVersion.dat
随机推荐
刷题笔记:二叉树剪枝(递归,迭代)
Analyze redis server
B树 和 B+树
强一致性和弱一致性的分析思路以及分布式场景的并发技巧
C# 自定义集合
Doubts about using the incluxdb database
Dynamic programming - "coin exchange problem"
Jetson TX1 installation pytoch
C#:快速排序,有相同的数字会忽略,然后继续先前的寻找方向去找下一个满足要求的数字进行替换
Flask项目中创建数据库表db.create_all()
C语言也能写植物大战僵尸
0数组 LeetCode605. 种花问题
使用InfluxDB数据库的疑惑
理解不能的锁们
Unity3D+moba+技能指示器(二)
Problems encountered in configuring the historical version of detectron
《wireshark网络分析就是这么简单》知识点与技巧
0动态规划 LeetCodde313. 超级丑数
【无标题】
C# 自定义栈(Stack)