Quickly learn to understand network protocols 4

4. Differences in how to establish a connection

4.1TCP

When it comes to establishing a connection with TCP, I believe that most people can definitely have a word in their minds, that's right -- "three-way handshake". TCP establishes a connection through a "three-way handshake", and then disconnects a connection through a "four-way handshake". What does TCP do with each wave?

The process is shown in the following figure (TCP's three-way handshake and four-way wave):
                Introduction to Network Programming for Lazy People (4): Quickly Understand the Difference Between TCP and UDP_1915184-43e91a9185faa031.jpg 

The above figure clearly shows TCP's three-way handshake and four-way wave from the perspective of the client and server. It can be seen that when TCP tries to establish a connection, the three-way handshake means that the client actively triggers it twice and the server triggers it once.
Can we first clarify what is the goal of TCP connection establishment and initialization?

  • 1) Initialize resources;
  • 2) Tell the other party my serial number.

So the order of the three-way handshake is as follows:

  • 1) The client side first sends a SYN packet to tell the server side that my initial serial number is X;
  • 2) After the server receives the SYN packet, it replies to the client with an ACK confirmation packet, telling the client that I have received it;
  • 3) Then the server also needs to tell the client its initial serial number, so the server also sends a SYN packet to tell the client that my initial serial number is Y;
  • 4) After the Client receives it, it replies to the Server with an ACK confirmation packet saying I know it.

Among them, steps 2 and 3 can be simplified into one step, that is to say, the ACK confirmation packet and the SYN serialization packet are sent to the client side together. So far, we have briefly explained the "three-way handshake" of TCP connection establishment.

4.2UDP

We all know that TCP is      a connection-oriented  reliable, and      ordered   byte stream  transport layer protocol , while UDP is a datagram -oriented , unreliable, and out-of-order transport protocol , so UDP will not establish any connection at all. .

Just like sending a text message, UDP only needs to know the IP address of the other party and send the datagrams one by one. Others, as senders, do not need to care.

(For articles on TCP's 3-way handshake and 4-way wave, please refer to " Theoretical Classics: Detailed Explanation of the 3-Way Handshake and 4-Way Process of TCP Protocol ", " Theory Linked to Practice: Wireshark Packet Capture Analysis TCP 3-way handshake, 4 times waving process ")

5. Differences in data transmission methods

Regarding the difference in data transmission between TCP and UDP, the biggest difference between the two can be reflected:

  • TCP:
    Since TCP is a protocol based on connections between two ends, there is no theoretical limit on the size of the data stream sent .

          However , due to the size limit of the buffer , if you use TCP to send a large piece of data , it may be truncated into several pieces, and the receiver will receive it in sequence .

  • UDP:
    Since UDP itself sends datagrams one by one , there is a natural upper limit .


So what factors determine the size of the datagram sent by UDP each time? ! ! ! ! ! ! ! ! ! ! ! ! important

  • 1. The UDP protocol itself , there is a 16-bit UDP packet length in the UDP protocol, so the UDP packet length cannot exceed 2^16=65536;
  • 2. The length of the Ethernet data frame, the MTU (maximum transmission unit) of the data link layer;
  • 3. UDP send buffer size of socket.


Let's first look at the first factor. The length of the UDP protocol itself is 2^16 - 1, the UDP header occupies 8 bytes, and the IP protocol itself is encapsulated and the header occupies 20 bytes, so the final length is: 2^16 - 1 - 20 - 8 = 65507 bytes.
Just looking at the first factor is a bit idealistic, because UDP is an unreliable protocol, and we should try to avoid data packets being fragmented during transmission. So there is a very important concept MTU -- that is, the maximum transmission unit.
The value of MTU under the Internet is 576 bytes, so the UDP protocol is used under the Internet, and the maximum number of bytes per datagram is: 576 - 20 - 8 = 548
(For the maximum packet length limit of the UDP protocol, see " What is the maximum size of a packet in UDP? 》)

 

6. Differences in data ordering

Let's talk about the orderliness of the data.

6.1TCP

For TCP, TCP itself has a series of complex algorithms such as timeout retransmission, error retransmission, and so on to ensure that TCP data is in order .

Suppose you send data 1, 2, and 3. As long as the sender and receiver remain connected, the data received by the receiver is always 1, 2, and 3.

6.2UDP

The UDP protocol is much more unrestrained, no matter how big the buffer pool is on the server side, the messages from the client side are always received one by one.

And due to the unreliability and disorder of UDP itself, if the client sends the three datagrams 1, 2, and 3, the server may receive the arrangement and combination of the three datagrams in any order and in any number.

7. Differences in reliability

In fact, we all know that TCP itself is a reliable protocol, while UDP is an unreliable protocol.

7.1TCP

Many algorithmic mechanisms within TCP make it very reliable in the process of maintaining a connection. For example: TCP timeout retransmission , error retransmission , TCP flow control , congestion control , slow warm start algorithm , congestion avoidance algorithm, fast recovery algorithm , etc.

So TCP is a protocol with complicated internal principles, but relatively simple to use.

7.2UDP

UDP is a non-connection-oriented protocol. Each datagram sent by UDP has its own IP address and the IP address of the receiver. It does not care whether the datagram is in error or not, as long as it is sent. .

So let's study, what will cause UDP packet loss:

  • 数据报分片重组丢失在文章之前我们就说过,UDP 的每个数据报大小多少最合适,事实上 UDP 协议本身规定的大小是 64kb,但是在数据链路层有 MTU 的限制,大小大概在 5kb,所以当你发送一个很大的 UDP 包的时候,这个包会在 IP 层进行分片,然后重组。这个过程就有可能导致分片的包丢失。UDP 本身有 CRC 检测机制,会抛弃掉丢失的 UDP 包;
  • UDP 缓冲区填满当 UDP 的缓冲区已经被填满的时候,接收方还没有处理这部分的 UDP 数据报,这个时候再过来的数据报就没有地方可以存了,自然就都被丢弃了。

 

8、使用场景总结

在文章最后的一部分,聊聊 TCP、UDP 使用场景。

先来说 UDP 的吧,有很多人都会觉得 UDP 与 TCP 相比,在性能速度上是占优势的。因为 UDP 并不用保持一个持续的连接,也不需要对收发包进行确认。

但事实上经过这么多年的发展 TCP 已经拥有足够多的算法和优化,在网络状态不错的情况下,TCP 的整体性能是优于 UDP 的。

那在什么时候我们非用 UDP 不可呢?

  • 对实时性要求高如实时会议实时视频这种情况下,如果使用 TCP,当网络不好发生重传时,画面肯定会有延时,甚至越堆越多如果使用 UDP 的话,即使偶尔丢了几个包,但是也不会影响什么,这种情况下使用 UDP 比较好;
  • Multipoint communication : TCP needs to maintain a long connection , so when multipoint communication is involved, it must establish its two-way connection with multiple communication nodes , and then sometimes in the NAT environment, two communication nodes establish their direct TCP connection is not An easy thing , and UDP can be sent directly without maintaining a connection, so the cost will be very low, and the penetration is good. In this case it is also correct to use UDP.

We talked about the usage scenarios of UDP above. In other cases, it is right to use TCP.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325305706&siteId=291194637