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.

  • tcp overview
  • TCP reliable data transmission
  • TCP traffic control
  • TCP connection management Let's have a brief review of tcp to summarize

TCP Overview

  • tcp is a point-to-point end-to-end transport protocol with a sender and a receiver.
  • tcp transfers a reliable stream of bytes that arrive in order
  • tcp uses a pipelining mechanism to improve the efficiency of the transmission. TCP controls the size of the sliding window through congestion control and flow control mechanisms
  • The tcp protocol sets up a sender cache and a receiver cache separately


  • tcp takes full-duplex (full-duplex) transmission, which means that during transmission, the same connection can transmit a two-way stream of data, from the sender to the receiver and from the receiver to the sender.
  • tcp is a connection-oriented protocol where both sides of a communication must establish a connection before sending data. Connection state is only maintained in both ends of the connection, and no state is maintained in nodes along the way. TCP connections include: cache on both hosts, connection state variables, sockets, etc.
  • tcp implements a traffic control mechanism

TCP segment structure


TCP: Sequence number and ACK

Serial number:

  • The sequence number refers to the number of the first byte in the segment, not the number of the segment
  • When establishing a TCP connection, both parties randomly select the sequence number


  • The sequence number of the next byte you wish to receive
  • Cumulative confirmation: all bytes before this sequence number have been correctly received

Q: How does the receiver handle Segments that arrive out of order?

  • A: Not specified in the TCP specification, decision to be made by TCP implementers


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

TCP reliable data transmission

The exact principle of reliable transmission is in the previous blog post Next we look at how the tcp protocol enables reliable transmission.

  • TCP implements reliable data transfer services on top of the unreliable services provided by the IP layer
  • Flowline mechanism
  • Cumulative confirmation
  • TCP uses a single retransmission timer
  • Events that trigger retransmissions: timeout and receipt of duplicate ACKs

RTT and timeout

Question: How do I set the timer timeout?

  • Greater than RTT, but RTT is variable
  • Too short: unnecessary retransmissions
  • Too long: response to segment loss time

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

TCP sender events

Upon receipt of data from the application layer, the following steps are performed.

  • Creating a segment
  • Sequence number is the number of the first byte of the segment
  • Turn on the timer.
  • Set the timeout time TimeOutInterval

If a timeout event occurs.

  • Retransmitting the segment that caused the timeout
  • Restart Timer

ACK received.

  • If a previously unconfirmed Segment is confirmed, update SendBase and restart the timer if there are still unconfirmed groups in the window

transmitter pseudocode

NextSeqNum = InitialSeqNum
SendBase = InitialSeqNum
loop (forever) {
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 */

TCP retransmission example




Fast retransmission mechanism

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

TCP traffic control

Receiver allocates buffer for TCP connection


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 connection management

TCP sender and receiver need to establish a connection before transmitting data.

Triple handshake mechanism

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


Four handshake mechanism

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.


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).

1、Robot to steal the legal job Chongqing set up artificial intelligence law school
2、International Hedge Fund Incubation Base Settles in Hefei Many Great Experts Talk About the Significance of Base Development
3、SuperMicro releases 40core dualcore Intel Xeon gaming concept for around 80k
4、Security audits guard data in the cloud
5、Star Ring Technology wins China Energy Enterprise Information Technology Product Technology Innovation Award

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送