Similarities and differences between TCP and UDP
TCP/IP
The transport layer of the model has two different protocols: UDP
user datagram protocol and TCP
transmission control protocol
Same point
TCP
AndUDP
protocols are running in the transport layerTCP
UDP
Open ports are required for communication with
difference
TCP
TCP
It is a connection-oriented protocol that provides full-duplex communication. It needs to establish a link before transmitting data, and the data transmission load is relatively large.TCP
Provide reliable delivery services, using services such as flow control and congestion control to ensure reliable communication.TCP
The smallest20
byte and the largest60
byte of the header include source port, destination port, serial number, confirmation number, data offset, control flag, window, checksum, emergency pointer, options and other information.TCP
Only one-to-one communication.TCP
For byte stream communication.TCP
To ensure the order of data transmission,TCP
determine the transmission order by coding a sequence number for each byte of the data stream in the connection.TCP
Provide functions such as checksum, confirmation response, sequence number, timeout retransmission, connection management, flow control, congestion control, etc.TCP
Suitable for applications that require reliable transmission, such as file transfer
UDP
UDP
It is connectionless, that is, there is no need to establish a connection before sending data, and the data transmission load is relatively small.UDP
Use best-effort delivery, which does not guarantee reliable delivery, and does not use flow control and congestion control.UDP
The first8
byte includes source port, destination port, length, checksum information.UDP
It has the functions of unicast, multicast and broadcast, and supports one-to-one, one-to-many, many-to-many, and many-to-one data transmission methods.UDP
It is oriented to message communication, and the messages handed over from the application layer are neither merged nor split, but the boundaries of these messages are retained and delivered to theIP
layer after the header is added .UDP
The order of data transmission is not guaranteed. The application layer program needs to add a sequence number to the data segment to control the order.UDP
OnlyIP
a few functions are added to the datagram service, that is, the function of the port and the function of error detection.UDP
Suitable for real-time applications, such as Internet telephony, video conferencing, live broadcast, etc.
related question
UDP realizes reliable transmission
The transport layer cannot guarantee the reliable transmission of data, and can only be achieved through the application layer. The implementation method can refer to the method of TCP
reliable transmission, but the implementation is not in the transmission layer, and the implementation is transferred to the application layer. , confirmation window mechanism to achieve reliable transmission, there is RUDP
, RTP
, UDT
like the use of open source UDP
to achieve reliable data transmission, in addition there are incorporated TCP
reliability and the UDP
speed tradeoff KCP
protocol.
The difference between TCP short connection and long connection
Short connection: Client
to Server
send a message, Server
respond Client
, and then write once completed, this time both sides can initiate any close
operation, but these are generally Client
the first to initiate close
the operation. Short connections generally only Client/Server
pass a read and write operation in between. Short links are relatively simple to manage. Establishing existing connections is a useful connection and does not require additional control methods. However, if customer requests are frequent, it will TCP
waste time and bandwidth on establishing and closing operations.
Long connection: Client
to Server
, after completion of a write, and the connection between them does not actively closed, subsequent read and write operations will continue to use the connection, the connection can be omitted more long TCP
operation and to establish a closed, reduce waste, conserve Time, for customers who frequently request resources, it is more suitable for long connections, but as the client connects more and more, the Server
load will become larger and larger. At this time, you need to adopt some strategies to reduce the load, such as turning off unread for a long time. Write link LRU
algorithm, limit the number of client links, etc.
TCP sticky packet unpacking solution
Since TCP
it is byte-stream oriented and cannot understand the business data of the upper layer, there is no guarantee that the data packet will not be split and reorganized at the bottom layer. This problem can only be solved by the design of the upper layer application protocol stack.
- Message fixed length: the sender encapsulates each data packet into a fixed length (if it is not enough, it can be
0
filled), so that each time the receiver reads a fixed length of data in the receive buffer, each data packet is naturally split Come. - Set the message boundary: the server separates the content of the message from the network stream according to the message boundary, and adds a carriage return and line feed character at the end of the packet for segmentation, such as a
FTP
protocol. - The message is divided into a message header and a message body: the message header contains a field indicating the total length of the message (or the length of the message body).
reference
https://zhuanlan.zhihu.com/p/108822858
https://www.cnblogs.com/liangyc/p/11628148.html
https://blog.csdn.net/m_xiaoer/article/details/72885418
https://blog.csdn.net/pangyemeng/article/details/50387078
https://blog.csdn.net/quiet_girl/article/details/50599777
https://blog.csdn.net/liuchenxia8/article/details/80428157
https://blog.csdn.net/qq_40732350/article/details/90902396
https://www.cnblogs.com/fundebug/p/differences-of-tcp-and-udp.html