The difference between TCP and UDP (ultra-detailed)

to sum up

  1. TCP is connection-oriented, UDP is connectionless
  2. TCP is a reliable, UDP is not reliable
  3. TCP is a byte-oriented stream, UDP datagram oriented
  4. TCP only supports point to point communication, UDP support one to one, one to many, many to many
  5. TCP packet header of 20 bytes, UDP header of 8 bytes
  6. TCP congestion control mechanism, UDP does not
  7. Under the TCP protocol the parties sending and receiving buffer has sent no practical significance buffer on UDP, but there receive buffer

TCP is connection-oriented, UDP is connectionless

When TCP protocol for network communications, we need to establish a connection, which means you need to first connect the client and server are properly connected, and then during the data exchange.

服务器的listen()函数和accept() 函数确保了连接的建立

Because when using the TCP protocol, Linux kernel stack creates two queues for the TCP connection

1. 半连接队列:用来保存处于SYN_SENT 和 SYN_RECV状态的请求
2. 全连接队列:accept队列,用来保存正在数据交互(established状态)的客户端连接

Full length == socket connection queue function listen () +1 second parameter

TCP is a reliable, UDP is not reliable

TCP uses it's own way of sending and receiving data to ensure reliable

  1. Packet check (checksum data 16) to prevent erroneous data is transmitted over the data
  2. Confirmation sequence number of the packets rearranged disorder
  3. Discards the duplicate packets, duplicate data redundancy to prevent
  4. Acknowledgment mechanism, the receiver will send a confirmation of acceptance of the data
  5. Timeout retransmission mechanism, issued after the data sender starts a timer, the timer exceeds the time still receives no acknowledgment, it will resend the data
  6. Flow control (16-bit window size) to ensure that receiver receives the data in the own buffer does not overflow
  7. Congestion control, to ensure the reliability of data propagating in the network, reduce the probability of packet loss, improve the reliability of the TCP

UDP does not above TCP mechanism
8. And if a checksum error, the UDP packet will be dropped

Byte stream oriented TCP, UDP datagram-oriented

Byte-oriented data stream is transmitted in units of byte, and a byte size data packets may be split into multiple packets, to facilitate transmission.
TCP can transmit data stored in the first buffer, waiting for data to be transmitted to a certain size, it can be sent directly, not to speak of a fixed size.
UDP requires each transmission requires a fixed length packet transmission, if too long application layer protocols need to cut to fit the active length.

TCP only supports point to point communication, UDP support one to one, one to many, many to many

TCP requires both sides to establish a connection, it is necessary to point to point communication.
UDP is no need to be so complicated.

TCP congestion control mechanism, UDP does not

TCP congestion control, but to ensure the reliability of the TCP transport protocol in the network, reduce the probability of retransmission and packet loss. UDP does not exist.

Under the TCP protocol the parties sending and receiving buffer has sent no practical significance buffer on UDP, but there receive buffer

Both are full-duplex, but need to send itself UDP data to be transferred to the Linux kernel, and then sent by the kernel. TCP is the use of a sliding window itself transmits data, and to ensure the reliability of data.

When selected TCP or UDP

For some real-time requirements is relatively high, the UDP choice, such as games, media communications, real-time video streaming (live), even if the transmission error can be tolerated; most other cases, HTTP is used the TCP, because the required transmission the contents reliable, does not appear lost

Published 52 original articles · won praise 26 · views 3398

Guess you like

Origin blog.csdn.net/weixin_43796685/article/details/104558965