TCP Protocol Overview of TCP TCP Reliable Data Transfer TCP Traffic Control TCP Connection Management
This article will briefly introduce the basic elements of the tcp protocol, which consists of four main parts.
Paste_Image.png
TCP segment structure
Paste_Image.png
TCP: Sequence number and ACK
Serial number:
ACKs:
Q: How does the receiver handle Segments that arrive out of order?
Paste_Image.png
Above we perform an analysis in order to figure out the application of tcp sequence numbers and ack First, hostA sends data to B as the sender, randomly selecting a sequence number seq = 42, which is the number of the first byte in this segment segment, and setting ack = 79, which indicates that the receiver is expected to send back seg = 79 as an acknowledgement signal that the receiver has accepted the segment correctly then (afterwards)HostB Data successfully received, Want the sender to return a confirmation message, Based on the sender'sack, So the confirmedseg=79, Then byack inform The sequence number of the next byte you wish to receive, and also indicates that all previous bytes have been correctly received, So sendack=43 Advise that it has been received43 The byte before the number, and wants the sender to transmit43 ordinal byte
The exact principle of reliable transmission is in the previous blog posthttp://www.jianshu.com/p/6a99944bb9fa Next we look at how the tcp protocol enables reliable transmission.
Question: How do I set the timer timeout?
Question: how to estimate RTT? SampleRTT: measures the time from when the segment was sent to when the ACK was received ignoring retransmissions SampleRTT variation,Measure multiple SampleRTTs, average them and form an estimate of RTT EstimatedRTT
EstimatedRTT = (1- )EstimatedRTT + SampleRTT Index-weighted moving average Typical value: 0.125
Upon receipt of data from the application layer, the following steps are performed.
If a timeout event occurs.
ACK received.
NextSeqNum = InitialSeqNum SendBase = InitialSeqNum loop (forever) { switch(event) event: data received from application above create TCP segment with sequence number NextSeqNum if (timer currently not running) start timer pass segment to IP NextSeqNum = NextSeqNum + length(data) event: timer timeout retransmit not-yet-acknowledged segment with smallest sequence number start timer event: ACK received, with ACK field value of y if (y > SendBase) { SendBase = y if (there are currently not-yet-acknowledged segments) start timer } } /* end of loop forever */
Paste_Image.png
Paste_Image.png
Paste_Image.png
In the TCP implementation, if a timeout occurs, the timeout interval is reset, i.e., the timeout interval is doubled, resulting in a large, long wait before retransmitting the lost packet. Packet loss is detected by duplicate ACKs, Sender will send multiple packets back to back, if a packet is lost, it may trigger multiple duplicate ACKs. If the sender receives 3 ACKs for the same data, it is assumed that the segment after that data is lost. Fast retransmission: retransmission before timer timeout algorithm
event: ACK received, with ACK field value of y if (y > SendBase) { SendBase = y if (there are currently not-yet-acknowledged segments) start timer } else { increment count of dup ACKs received for y if (count of dup ACKs received for y = 3) { resend segment with sequence number y }
Receiver allocates buffer for TCP connection
Paste_Image.png
Upper layer applications may be slower to process data in buffer
Flow control: the sender does not transmit so much, so fast, that it overwhelms the receiver (buffer overflow)
(assuming the TCP receiver discards the garbled segments) Space available in Buffer (spare room) = RcvWindow = RcvBuffer-[LastByteRcvd -LastByteRead]
The Receiver tells Sender about the RcvWindow by telling it in the Segment's header field. Sender limits the data it has sent but has not yet received an ACK to no more than the receiver's free RcvWindow size.
Receiver tells SenderRcvWindow=0,what happens? There will be a jam and the sender will not send data. The specifics about these issues will be discussed inside tcp congestion control.
TCP sender and receiver need to establish a connection before transmitting data.
Step 1: client host sends TCP SYN segment to server specifies initial seq # no data Step 2: server host receives SYN, replies with SYNACK segment server allocates buffers specifies server initial seq. # Step 3: client receives SYNACK, replies with ACK segment, which may contain data
Paste_Image.png
TCP connection management: close
Step 1: client sends TCP FIN control-segment to server Step 2: server receives FIN, replies with ACK. Close connection, send FIN. Step 3: client receives FIN, replies with ACK. Go to "wait" - if a FIN is received, an ACK will be resent Step 4: server receives ACK. Connection closed.
Paste_Image.png
Since TCP connections are full-duplex, each direction must be closed separately. The principle is that when a party has completed its data sending task it can send a FIN to terminate the connection in that direction. Receiving a FIN only means that no data is flowing in that direction, and a TCP connection can still send data after receiving a FIN. The first party to perform the shutdown will perform the active shutdown, while the other party performs the passive shutdown. (1) The TCP client sends a FIN which is used to close the data transfer from the client to the server (message segment 4). (2) The server receives this FIN, and it sends back an ACK with the acknowledgement sequence number of the received sequence number plus one (message segment 5). As with SYN, a FIN will occupy a sequential number. (3) The server closes the client's connection and sends a FIN to the client (message segment 6). (4) The client segment sends back an ACK message acknowledgement and sets the acknowledgement sequence number to the received sequence number plus one (message segment 7).