Computer network notes: TCP protocol and UDP protocol (transport layer)

insert image description here

Both TCP and UDP are transport layer protocols, and they both belong to the TCP/IP protocol family.

TCP

basic concept

The full name of TCP is 传输控制协议a communication 面向连接的、可靠的、基于字节流protocol 传输层. TCP is a connection-oriented, reliable stream protocol (stream is an uninterrupted data structure)

TCP packet format

TCP报文It is the data unit transmitted by the TCP layer, also known as 报文段, a TCP segment consists of two parts 首部and 数据段two parts. Among them, the header is the control information added by TCP in order to achieve end-to-end reliable transmission, and the data segment part is the data from the upper layer, that is, the application layer.
insert image description here

  • 源端口和目标端口: Write source port number and destination port number respectively, support TCP multiplexing mechanism.
  • 发送序号: In order to ensure the correctness of data transmission, TCP carries out a 32-bit serial number for each transmitted byte. This number does not necessarily start from 0. The value of the serial number field in the header refers to the data sent in this segment The number of the first byte of .
  • 确认序号: The acknowledgment sequence number is also called the reception sequence number, which is the number of the first data byte expected to receive the next segment of the other party.
  • 首部长度: Occupies 4 bits, indicating the header length of the TCP message (from the vertical view in Figure 3-10, the unit is 32 bits, indicating that the TCP message header contains five 32-bit fixed headers, that is, 20 bytes fixed header), since the maximum decimal number represented by a 4-digit binary number is 15, the maximum value of the data offset is (4 bytes × 15) 60 bytes, which is the maximum length of the TCP header, that is, the option length cannot more than 40 bytes.
  • 保留: 6 unused bits, reserved for future applications, currently set to "0".
  • 6个控制位: Complete the main transmission control functions of TCP (such as session establishment and termination).
    insert image description here
  • 窗口: This field defines the window size (in bytes) that the peer must maintain. Note that the length of this field is 16 bits, which means that the maximum length of the window is 65 535 bytes. This value is usually called the receive window, and it is determined by the receiver. At this time, the sender must obey the control of the receiver.
  • 检验和: This 16-bit field contains the checksum. The calculation process of the TCP checksum is the same as that used for the UDP checksum described above. However, checksums are optional in UDP datagrams, whereas inclusion of checksums is mandatory for TCP.
  • 紧急指针: Gives the offset from the current sequence number to the urgent data location.
  • 选项: Provide a method to add additional settings, the length is variable, up to 40 bytes, when this option is not used, the TCP header length is 20 bytes.
  • 填充: When the length of the option field is less than 32-bit word length, several bytes of all 0 padding will appear at the end of the TCP header.
  • 数据: Protocol data from the upper layer, namely the application layer.

features

  • 面向连接

Connection-oriented means that a connection must be established at both ends before sending data. The method of establishing a connection is "three-way handshake", which can establish a reliable connection. Establishing a connection lays the foundation for reliable data transmission.

  • 仅支持单播传输

Each TCP transmission connection can only have two endpoints, and can only perform point-to-point data transmission, and does not support multicast and broadcast transmission methods.

  • 面向字节流

Unlike UDP, TCP does not transmit individual packets independently, but transmits them in byte streams without retaining packet boundaries.

  • 可靠传输

For reliable transmission, the judgment of packet loss and bit error depends on the segment number and confirmation number of TCP. In order to ensure the reliability of message transmission, TCP gives each packet a sequence number, and the sequence number also ensures the sequential reception of the packets transmitted to the receiving entity. The receiving entity then sends back a corresponding acknowledgment (ACK) for the successfully received bytes; if the sending entity does not receive an acknowledgment within a reasonable round-trip delay (RTT), then the corresponding data (assumed to be lost) will be retransmitted.

  • 提供拥塞控制

When the network is congested, TCP can reduce the rate and amount of data injected into the network to relieve congestion.

  • 提供全双工通信

TCP allows applications on both sides of the communication to send data at any time, because both ends of the TCP connection have buffers for temporarily storing data for two-way communication. Of course, TCP can send a data segment immediately, or it can buffer for a period of time to send more data segments at a time (the maximum data segment size depends on MSS)

TCP retransmission mechanism

Since the underlying network (network layer) of TCP may be lost, duplicated or out of order, the TCP protocol provides reliable data transmission services. In order to ensure the correctness of data transmission, TCP will retransmit the packets it thinks are lost (including bit errors in the message). TCP uses two independent mechanisms to complete the retransmission, one is 基于时间, and the other is 基于确认信息.

After TCP sends a piece of data, it starts a timer. If it does not receive an ACK confirmation message for sending data within this time, it will retransmit the message, and give up and send a message when it reaches a certain number of times without success. reset signal.

TCP congestion control mechanism

insert image description here

  • slow start (slow start)

Set when starting to send cwnd = 1(cwnd refers to the congestion window)

Idea: Don't send a lot of data at the beginning, but test the congestion level of the network first, and increase the size of the congestion window from small to large. In order to prevent network congestion caused by excessive growth of cwnd, a slow start threshold (ssthresh state variable) is set.

When cnwd < ssthresh, use the slow start algorithm
When cnwd = ssthresh, use both the slow start algorithm and the congestion avoidance algorithm
When cnwd > ssthresh, use the congestion avoidance algorithm

  • congestion avoidance

Congestion avoidance may not be able to completely avoid congestion. It means that the congestion window is controlled to grow linearly during the congestion avoidance phase, making the network less prone to congestion.

Idea: Let the congestion window cwnd increase slowly, that is, increase the congestion control window of the sender by one every time a return time RTT passes. Whether in the slow start phase or the congestion avoidance phase, as long as the sender judges that the network is congested, the slow start threshold is set to half the size of the sending window when congestion occurs. Then set the congestion window to 1 and execute the slow start algorithm.

  • fast retransmit

Fast retransmission requires the receiver to send a repeated acknowledgment immediately after receiving an out-of-sequence segment (in order to let the sender know early that a segment has not reached the other party). As long as the sender receives three consecutive repeated acknowledgments, it will immediately retransmit the message segment that the other party has not yet received, without having to continue to wait for the set retransmission timer to expire.

Since there is no need to wait for the set retransmission timer to expire, unacknowledged message segments can be retransmitted as soon as possible, which can improve the throughput of the entire network

  • quick recovery

When the sender receives three repeated confirmations in a row, “乘法减小”the algorithm is executed to ssthresh门限halve. But then the slow start algorithm is not executed.

Considering that if the network is congested, it will not receive several duplicate acknowledgments, the sender now believes that the network may not be congested. Therefore, the slow start algorithm is not implemented at this time, but the size cwndis set to ssthresh, and then the congestion avoidance algorithm is executed.

TCP flow control mechanism

Generally speaking, 流量控制it is to let the sender not send data too fast, so that the receiver can receive it in time. TCP uses variable size 滑动窗口for flow control, 窗口大小的单位是字节. 窗口大小That's what it says here 每次传输的数据大小.

Window Concept Diagram
insert image description here

  • aThe sender wants to send 900-byte long data, which is divided into nine 100-byte long message segments, and the sending window determined by the other party is 500 bytes. As long as the sending end receives the confirmation from the other party, the sending window can be moved forward. The TCP at the sending end maintains a pointer, and every time a segment is sent, the pointer moves forward by a segment distance. When the pointer moves to the rightmost end of the sending window (that is, the front edge of the window), the segment can no longer be sent.

  • bThe sender has sent 400 bytes of data, but only received an acknowledgment for the first 200 bytes of data, and the window size remains unchanged. We noticed that the sender can still send 500-200=300 bytes now.

  • cThe sender has received the other party's acknowledgment of the first 400 bytes of data, but the window is reduced to 400 bytes, so the sender can still send 400 bytes of data.

Flow Control Mechanism Process

  • When a connection is established, each end of the connection allocates a buffer to hold incoming data and sends the size of the buffer to the other end.

  • When the data arrives, the receiver sends an acknowledgment, which contains its remaining buffer size. (The size of the remaining buffer space is called the window, and notifications indicating the size of the window are called window advertisements. The receiver includes a window advertisement in every acknowledgment it sends.)

  • If the receiver application can read the data as fast as the data arrives, the receiver will send a positive window notification with each acknowledgment.

  • If the sender operates faster than the receiver, the received data will eventually fill the receiver's buffer, causing the receiver to advertise a zero window. When the sender receives a zero-window advertisement, it must stop sending until the receiver re-advertises a positive window.

TCP's Reliable Transport Mechanism

TCP 的可靠传输机制Yes 基于连续 ARQ 协议and 滑动窗口协议.

The TCP protocol maintains a sending window on the sender. The message segments before the sending window are the message segments that have been sent and confirmed. The message segments after the sending window are the message segments that are not allowed to be sent in the cache. When the sender sends a message to the receiver, it will send all the message segments in the window in sequence, and set a timer, which can be understood as the earliest message segment sent but not received confirmation. If the confirmation reply of a certain message segment is received within the time of the timer, then slide the window, and slide the head of the window backward to the last position of the confirmation message segment. If there is no message segment, reset the timer, and if there is no more, turn off the timer. If the timer expires, resend all the 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 acknowledgment responses from the receiver, this is an indication that the message segment after this message segment is likely to be lost, then the sender will enable the fast retransmission mechanism , that is, send all sent but confirmed segments before the current timer expires.

The receiver uses a cumulative acknowledgment mechanism. For all message segments that arrive in sequence, the receiver returns an affirmative answer of a message segment. If an out-of-order segment is received, the receiver discards it and returns an affirmative answer to the most recent segment that arrived in order. The use of cumulative confirmation ensures that the message segments before the returned confirmation number have arrived in order, so the sending window can be moved to the back of the confirmed message segment.

The size of the sending window is variable, and it is determined by the remaining size of the receiving window and the degree of congestion in the network. TCP controls the sending rate of message segments by controlling the length of the sending window.
But the TCP protocol is not exactly the same as the sliding window protocol, because many TCP implementations will cache out-of-order segments, and when retransmission occurs, only one segment will be retransmitted, so the reliable transmission mechanism of the TCP protocol It is more like a hybrid of window sliding protocol and selective retransmission protocol.

UDP

basic concept

The full name of UDP is that 用户数据报协议it is used to process data packets in the same way as the TCP protocol in the network, and it is a unique 无连接protocol. In the OSI model, at the transport layer, it is at the upper layer of the IP protocol. UDP has the disadvantages of not providing data packet grouping, assembling and sorting of data packets, that is to say, after the message is sent, it is impossible to know whether it has arrived safely and completely.

features

  • 面向无连接

First of all, UDP does not need to perform a three-way handshake to establish a connection before sending data like TCP. If you want to send data, you can start sending. And it is only a porter of the data message, and will not perform any splitting and splicing operations on the data message.

Specifically:

At the sending end, the application layer passes the data to the UDP protocol of the transport layer. UDP will only add a UDP header to the data, which is the UDP protocol, and then pass it to the network layer. At the receiving end, the network layer passes the data to the transport layer
. , UDP only removes the IP header and passes it to the application layer without any splicing operation

  • 有单播,多播,广播的功能

UDP not only supports one-to-one transmission mode, but also supports one-to-many, many-to-many, and many-to-one modes, that is to say, UDP provides unicast, multicast, and broadcast functions.

  • 面向报文

The sender's UDP sends the message to the application program, and after adding the header, it is delivered to the IP layer. UDP neither merges nor splits the packets delivered by the application layer, but preserves the boundaries of these packets. Therefore, the application must choose the appropriate size of the message

  • 不可靠性

First of all, the unreliability is reflected in the fact that there is no connection. The communication does not need to establish a connection, and it can be sent as soon as it is wanted. Such a situation is definitely unreliable.

And it will transmit whatever data is received, and it will not back up the data, and it will not care whether the other party has received the data correctly when sending the data.

Furthermore, the network environment is good and bad, but because UDP has no congestion control, it will always send data at a constant speed. Even if the network condition is bad, the sending rate will not be adjusted. The disadvantage of this implementation is that it may cause packet loss when the network conditions are not good, but the advantage is also obvious. In some scenarios with high real-time requirements (such as conference calls), you need to use UDP instead of TCP.

  • 头部开销小,传输数据报文时是很高

insert image description here
The UDP header contains the following data:

1、两个十六位的端口号,分别为源端口(可选字段)和目标端口
2、整个数据报文的长度
3、整个数据报文的检验和(IPv4 可选字段),该字段用于发现头部信息和数据中的错误

Therefore, the header overhead of UDP is small, only 8 bytes, much less than TCP's at least 20 bytes, and it is very efficient when transmitting data packets.

Usage scenarios of TCP and UDP

TCP application scenarios

Scenarios with relatively low efficiency requirements but relatively high accuracy requirements. Because operations such as data confirmation, retransmission, and sorting are required during transmission, the efficiency is not as high as that of UDP.
For example: file transfer (high accuracy and high requirements, but the speed can be relatively slow), accepting mail, remote login.

UDP application scenarios

Scenes with relatively high efficiency requirements and relatively low accuracy requirements.
For example: QQ chat, online video, VoIP (instant messaging, high speed requirements, but occasional interruption is not a big problem, and the retransmission mechanism cannot be used here), broadcast communication (broadcast, multicast).

Why is the UDP protocol unreliable?

UDP does not need to establish a connection before transmitting data, and the transport layer of the remote host does not need to confirm after receiving the UDP message, providing unreliable delivery. To sum up the following four points:

  • No guarantee of message delivery: no acknowledgment, no retransmission, no timeout
  • Delivery order is not guaranteed: no packet sequence number is set, no rearrangement, no front-of-line blocking occurs
  • Does not track connection state: no need to establish a connection or restart the state machine
  • No congestion control: no built-in client or network feedback mechanism

おすすめ

転載: blog.csdn.net/weixin_40119412/article/details/130455004