当前位置:网站首页>TCP stuff
TCP stuff
2022-06-25 08:07:00 【Shrek】
List of articles
Transport layer
The transport layer is Application process Provide end-to-end logical communication .( Add : The network layer by Between the host Provide logical communication .)
The transport layer has two different transport protocols , Connection oriented TCP And unconnected UDP
TCP Header format of message segment
Learn some TCP Common fields in header :
ACK: confirm ACK, 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 transmitted message segments must be ACK Set as 1.
SYN: Sync SYN, Used to synchronize sequence Numbers when connection is established . When SYN = 1 and ACK=0 when , Indicates that this is a connection request segment . The other party agrees to establish a connection , Then... Shall be made in the corresponding message segment SYN=1 and ACK = 1. therefore ,SYN Set as 1 This represents a connection request or connection acceptance message .
FIN: 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 asks to release the transport connection .
TCP Reliable transmission mechanism
TCP The reliable transmission mechanism is based on continuous transmission ARQ Protocol and sliding window protocol .
TCP The protocol maintains a sending window at the sender , The message segment before the sending window is the one that has been sent and confirmed , The send window contains Sent but unacknowledged message segment and A message segment that is allowed to be sent but has not yet been sent , The message segment after the sending window is one that is not allowed to be sent in the cache .
When The sender towards The receiving party When sending a message , All message segments in the window will be sent in turn , And set a timer , This timer can be understood as the earliest sent but not received message segment . If within the time of the timer Receive a confirmation answer of a message segment , Then slide the window , Slide the head of the window back to the next position in the acknowledgement segment , At this time, if there are message segments that have been sent but not confirmed , Reset the timer , If not, turn off the timer .
If the timer times out , Then resend all message segments that have been sent but have not received confirmation , And set the timeout interval to twice the previous one . When the sender receives three redundant acknowledgement replies from the receiver , It's an indication , It indicates that the message segment after the message segment is likely to be lost , Then the sender will enable the fast retransmission mechanism , Before the end of the current timer , Send all sent but confirmed message segments .
The receiver uses a cumulative acknowledgement mechanism , For all segments arriving in sequence , The receiver returns a positive answer to a message segment . If an out of order message is received paragraph , Then the receiver will discard it directly , A message that answers the most recent message arrives in sequence . The cumulative acknowledgement ensures that the message segments before the returned acknowledgement number are It has arrived in order , So the sending window can be moved to the back of the confirmed message segment .
The size of the send window varies , It is determined by the remaining size of the receiving window and the degree of congestion in the network ,TCP It is to control the message by controlling the length of the sending window The transmission rate of segments .
however TCP The protocol is not exactly the same as the sliding window protocol , Because a lot of TCP The implementation will cache out of order segments , And retransmission occurs , It's just heavy Send a message segment , therefore TCP The reliable transmission mechanism of the protocol is more like a mixture of window sliding protocol and selective retransmission protocol
TCP Flow control mechanism of
Generally speaking , Flow control is to make The sender Don't send data too fast , Must let The receiving party Time to receive .TCP Adopt variable size The sliding window Flow control , Window size In bytes . The window size mentioned here is actually the data size transmitted each time .
- When the connection is established , Each end of the connection allocates a buffer to hold the input data , And send the size of the buffer to the other end .
- When data arrives , The receiver sends a confirmation , It contains the remaining buffer size .( The size of the remaining buffer space is called window , A notification indicating the size of a window is called a window notification . Each acknowledgement sent by the receiver contains a window notification .)
- If the receiving application can read data as fast as the data arrives , The receiver will send a positive window notification in each confirmation .
- If the sender operates faster than the receiver , The received data will eventually fill the receiver's buffer , Cause the receiver to announce a zero window . When the sender receives a zero window notification , Must stop sending , Until the receiver notifies a positive window again .
TCP How to ensure the orderly and reliable transmission of data packets ?
Segment and number the byte stream, and then pass ACK Reply and timeout retransmission are two mechanisms to ensure .
(1) In order to ensure the reliable transmission of data packets , The sender must keep the sent packets in the buffer .
(2) And start a timeout timer for each sent packet
(3) If you receive a response message from the other party before the timer expires ( It may be a response to this package , It can also be a response to subsequent packages of this package ), Then release the buffer occupied by the packet ;
(4) otherwise , Retransmit the packet , Until the number of replies or retransmissions exceeds the specified maximum number .
(5) After receiving the packet , to CRC check , If it is correct, the data will be handed over to the upper layer protocol , Then send a cumulative response packet to the sender , Indicates that the data has been received , If the receiver is good, there is also data to be sent to the sender , The reply packet can also be carried in the data packet .
TCP The retransmission mechanism
because TCP Lower network of ( The network layer ) May appear The loss of 、 Repetition or disorder The situation of ,TCP The protocol provides reliable data transmission services . To ensure the correctness of data transmission ,TCP It will retransmit what it thinks has been lost ( Including bit errors in messages ) My bag .TCP Two independent mechanisms are used to complete retransmission , One is Based on time , The second is based on Confirmation information .
TCP After sending a data , Just turn on a timer , If you don't receive the sent data within this time ACK Confirmation message , Then retransmit the message , When a certain number of times have not been successful, give up and send a reset signal .
TCP Three handshakes

Three handshakes In fact, it means building a TCP When the connection , Need client and server to send in total 3 A package . The main function of three handshakes is to confirm the Receptivity and Sending capability If there is something wrong 、 Specify your own initialization serial number to prepare for later reliable transfer . In essence, it is to connect to the specified port of the server , establish TCP Connect , And connect the serial number and confirmation number of both sides synchronously , In exchange for TCP Window size information .
At first, the client is in closed The state of , The server is in Listen state , Make three handshakes
The first handshake : The client sends a... To the server SYN message , At this time, the same part in the head SYN = 1, At the same time, select an initial sequence number seq = x .TCP Regulations ,SYN Message segment ( namely SYN = 1 Message segment ) Can't carry data , But we need to consume a sequence number . this when TCP The client process enters SYN-SENT( The synchronous ) state .
The second handshake : The server received... From the client SYN After the message , Will take their own SYN Message as reply , The server shall put... In the confirmation message segment SYN Bit and ACK All positions are set 1, The confirmation number is ack = x + 1, Also choose an initial sequence number for yourself seq = y . This segment can't carry data either , But also consume a sequence number . At this time TCP The server process enters SYN-RCVD( Sync received ) state .
The third handshake : TCP After receiving the confirmation from the server, the customer process should also confirm to the server . Confirm the ACK Set as 1, Confirmation no. ack = y + 1, And their own serial number seq = x + 1( For the initial seq = x, So the second segment is +1).ACK The message segment can carry data , Without data, no serial number will be consumed . At this time TCP Connection established ,A Get into ESTABLISHED( Connection established ) state . When the server receives the confirmation from the client , Also enter ESTABLISHED state .
What is the function of three handshakes ?
- The first handshake : The client sends network packets , The server received it .
Then the server can draw a conclusion : Client sending capability 、 The reception capability on the server side is normal . - The second handshake : Server sends packets , The client received it .
This way the client can draw a conclusion : Server side reception 、 Sending capability , Client reception 、 The ability to send is normal . However, at this time, the server cannot confirm whether the acceptance ability of the client is normal . - The third handshake : Client sends packets , The server received it .
Then the server can draw a conclusion : Client reception 、 Normal sending capacity , Server's own send 、 Reception is normal .
Can I carry data during the three handshakes ?
Actually, the third time I shook hands , It can carry data . however , for the first time 、 The second handshake is not allowed to carry data
Why is that ? You can think of a question , If the first handshake can carry data , If someone wants to attack the server maliciously , Then every time he was in the first handshake SYN Put a lot of data in the message . Because the attacker doesn't care about the server's reception at all 、 Whether the sending ability is normal , And then crazy repeat SYN Message words , It's going to take a lot of time for the server 、 Memory space to receive these messages .
in other words , You can't put data in the first handshake , One simple reason is that it makes the server more vulnerable . And for the third time , The client is already in ESTABLISHED state . For the client , He has established a connection , And we already know the server's reception 、 The ability to send is normal , So there's nothing wrong with being able to carry data .
TCP Four waves

Three handshakes are required to establish a connection , And it takes four waves to terminate a connection ( There are also four waves called four handshakes ). This is due to TCP Half closed (half-close) Caused by the . So called half closed , In fact, that is TCP It provides the ability that one end of the connection can receive data from the other end after its transmission .
TCP The removal of the connection requires sending four packets , So called Four waves (Four-way handshake), The client or the server can take the initiative to wave .
At first both sides were in ESTABLISHED state , If it is the client that initiates the shutdown request first . The process of four waves is as follows :
- First wave :
clientProcess first to its TCP Send connection release message segment , And stop sending data , Active shut down TCP Connect .clientThe termination control bit of the header of the message segment that releases the connection FIN Set up 1, Its serial number seq = u, It is equal to the sequence number of the last byte of the previously transmitted data plus 1. At this time client Get into FIN-WAIT-1( Stop waiting 1) state , wait forThe serverThe confirmation of . Please note that ,TCP Regulations ,FIN Even if the message segment does not carry data , It also consumes a serial number . - Second wave :
The serverAfter receiving the connection release message segment, a confirmation is sent , The confirmation number is ack = u + 1, And the sequence number of this message segment is v, be equal toclientThe sequence number of the last byte of the data that has been transmitted before plus 1. then The server enters CLOSE-WAIT( Turn off waiting ) state .TCP The server process should then notify the high-level application process , So fromclientToThe serverThe connection in this direction is released . At this time TCP The connection is half closed , namelyclientThere is no data to send , butThe serverIf you send data ,clientStill want to receive . in other words , fromThe serverToclientThe connection in this direction is not closed , This state may last for some time .clientReceived fromThe serverAfter the confirmation of , To get into FIN-WAIT-2( Stop waiting 2) state , wait forThe serverSent connection release message segment . - Third wave : if
The serverThere is no need to ask A Data sent , Its application process will inform TCP Release the connection . At this timeThe serverThe outgoing connection release message segment must enable FIN = 1. Now supposeThe serverThe serial number of is w,( In the half closed stateThe serverSome data may have been sent ).The serverThe confirmation number sent last time must be repeated ack = u + 1. At this timeThe serverIt's in LAST-ACK( Final confirmation ) state , wait forclientThe confirmation of . - Fourth wave :
clientUpon receipt of b The connection of · After releasing the message segment , This must be confirmed . Put... In the confirmation message segment ACK Set up 1, Confirmation no. ack = w + 1, And my serial number is seq = u + 1( according to TCP standard , Sent earlier FIN The segment consumes a sequence number ). And then into TIME-WAIT( Time waits ) state . Please note that , Now? TCP The connection has not been released , It has to take time for the timer (TIME-WAIT timer) Setup time 2MSL after ,clientJust enter into CLOSED state .
Time MSL It's called the longest segment life .
边栏推荐
- RMQ interval maximum subscript query, interval maximum
- 【莫比乌斯反演】
- 三台西门子消防主机FC18配套CAN光端机进行光纤冗余环网组网测试
- 电子学:第013课——实验 14:可穿戴的脉冲发光体
- 电子学:第012课——实验 13:烧烤 LED
- 使用报文和波形记录分析仪RoyalScope的帧统计功能排查CAN总线偶发性故障
- Luogu p2048 [noi2010] super Piano (rmq+ priority queue)
- MySQL simple permission management
- [supplementary question] 2021 Niuke summer multi school training camp 9-N
- 现在通过开户经理发的开户链接股票开户安全吗?
猜你喜欢

Drawing of clock dial

电子学:第012课——实验 13:烧烤 LED

初体验完全托管型图数据库 Amazon Neptune

Electronics: Lesson 010 - Experiment 9: time and capacitors

使用报文和波形记录分析仪RoyalScope的帧统计功能排查CAN总线偶发性故障

电子学:第008课——实验 6:非常简单的开关

50 pieces of professional knowledge of Product Manager (IV) - from problem to ability improvement: amdgf model tool

取消word文档中某些页面的页眉

Anaconda based module installation and precautions

To understand the difference between Gram-positive and Gram-negative bacteria and the difference in pathogenicity
随机推荐
Machine learning notes linear regression of time series
Modeling and fault simulation of aircraft bleed system
Electronics: Lesson 011 - experiment 10: transistor switches
剑指offer刷题(中等等级)
力扣 272. 最接近的二叉搜索树值 II 递归
CVPR 2022 Oral 2D图像秒变逼真3D物体
Talk about the future of cloud native database
MySQL simple permission management
Determine whether the user is entering a page for the first time
三台西门子消防主机FC18配套CAN光端机进行光纤冗余环网组网测试
Mining microbial dark matter -- a new idea
MySQL简单权限管理
現在通過開戶經理發的開戶鏈接股票開戶安全嗎?
Anaconda based module installation and precautions
使用报文和波形记录分析仪RoyalScope的帧统计功能排查CAN总线偶发性故障
c#中设置lable控件的TextAlign属性控制文字居中的方法
协议和服务的区别?
剑指offer刷题(简单等级)
【补题】2021牛客暑期多校训练营1-3
洛谷P5994 [PA2014]Kuglarz(异或思维+MST)