Transport layer
1. Services provided by the transport layer
1.1, the function of the transport layer
- The transport layer provides communication between processes (ie end-to-end communication)
- Multiplexing and demultiplexing
- Error detection (header and data part) , the network layer only performs header detection
- Provide two different communication protocols
1.2. Addressing and ports of the transport layer
Socket
2. UDP protocol
2.1, UDP datagram
UDP just adds two most basic services to the IP datagram service: multiplexing and demultiplexing, and error detection .
UDP provides best-effort delivery, that is, reliable delivery is not guaranteed, so the work of maintaining transmission reliability requires users to complete the application layer .
UDP is message-oriented . Complete a complete message at a time, and the message is the smallest unit of UDP transmission.
2.2, UDP header format
2.3, UDP verification
When calculating the checksum, a pseudo header of 12B should be added before the UDP datagram .
Throw it away if you make a mistake .
3. TCP protocol
TCP is a reliable data transmission protocol implemented on top of the unreliable IP layer. It mainly solves the problems of reliability, order, no loss and no duplication of transmission . Features:
- Connection-oriented
- Provide reliable delivery services;
- Full duplex communication, with sending buffer and receiving buffer;
- TCP is oriented towards byte streams.
3.1, TCP segment
The data unit transmitted by TCP is called a message segment . A TCP message segment is divided into two parts: TCP header and TCP data . The entire TCP segment is encapsulated in the IP datagram as the data part of the IP datagram. The 20B of the header is fixed .
Field name | Number of digits | Description |
---|---|---|
Source port number and destination port number | 16 positions each | Source port number and destination port number |
Sequence number field seq | 32nd place | Each byte in the data stream transmitted by the TCP connection is coded with a sequence number. The value of the sequence number field refers to the sequence number of the first byte of the data sent in this segment |
Confirmation number field ack | 32nd place | It is the sequence number of the first byte expected to receive the data of the next segment of the other party . If the confirmation number is N, it means that all data up to the sequence number N-1 has been received correctly |
Data offset | 4th | It points out how far the data start of the TCP segment is from the start of the TCP segment |
ACK | First place | The acknowledgment number field is valid only when ACK=1 . TCP stipulates that ACK must be set to 1 in all message segments transmitted after the connection is established. |
Sync bit SYN | First place | Synchronous SYN=1 means that this is a connection request (SYN=1, ACK=0) or connection reception message (SYN=1, ACK=1) |
Stop bit FIN | First place | Used to release a connection . FIN=1 indicates that the data of the sender of this segment has been sent, and the transmission connection is required to be released |
Checksum | 16th place | Also add the pseudo header of 12B in front |
3.2, TCP connection management
3.2.1, TCP connection establishment (three-way handshake)
- SYN=1 seq=x
- SYN = 1 ACK = 1 seq = y ack = x + 1
- ACK = 1 seq = x + 1 ack = y + 1
3.2.2, TCP connection release (four waves of hands)
- FIN=1 seq=u
- ACK = 1 seq = v ack = u + 1
- FIN = 1 ACK = 1 seq = w ack = u + 1
- ACK = 1 seq = u + 1 ack = w + 1
3.3, TCP reliable transmission
TCP uses mechanisms such as verification, sequence number, confirmation, and retransmission to ensure reliable transmission .
3.3.1, verification
Same as UDP
3.3.2, serial number
The sequence number field of the TCP header ensures that the data can be submitted to the application layer in an orderly manner. TCP treats the data as an unstructured but ordered byte stream, and the sequence number is established on the transmitted byte stream .
3.3.3, confirmation
The acknowledgment number of the TCP header is the sequence number of the first byte expecting to receive the data of the next segment of the other party .
TCP uses cumulative confirmation by default .
3.3.4, retransmission
Two events that cause TCP retransmission: timeout and (3) redundant ACKs .
3.4, TCP flow control
TCP provides a flow control mechanism based on a sliding window to eliminate the possibility of overflowing the receiver's buffer.
The receiver dynamically adjusts the size of the sender's sending window according to the size of its receiving buffer, which is called the receive window rwnd ; at the same time, the sender determines the window value based on its estimation of network congestion, which is called congestion Window cwnd . The sending window of the sender is usually equal to the minimum of rwnd and cwnd .
3.5, TCP congestion control
The TCP protocol requires the sender to maintain two windows:
Receive window rwnd : The latest window value promised by the receiver according to the current receiving buffer size, reflecting the receiver's capacity . The receiver informs the sender according to the window field placed in the header of the TCP message
Congestion window cwnd : The window value set by the sender according to the degree of network congestion estimated by the sender, reflecting the current capacity of the network.
The upper limit of the sending window should be the smaller of rwnd and cwnd, that is: the upper limit of the sending window=min[rwnd,cwnd]