Similarities and differences between TCP and UDP

Similarities and differences between TCP and UDP

TCP/IPThe transport layer of the model has two different protocols: UDPuser datagram protocol and TCPtransmission control protocol

Same point

  • TCPAnd UDPprotocols are running in the transport layer
  • TCPUDPOpen ports are required for communication with

difference

TCP

  • TCPIt 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.
  • TCPProvide reliable delivery services, using services such as flow control and congestion control to ensure reliable communication.
  • TCPThe smallest 20byte and the largest 60byte of the header include source port, destination port, serial number, confirmation number, data offset, control flag, window, checksum, emergency pointer, options and other information.
  • TCPOnly one-to-one communication.
  • TCPFor byte stream communication.
  • TCPTo ensure the order of data transmission, TCPdetermine the transmission order by coding a sequence number for each byte of the data stream in the connection.
  • TCPProvide functions such as checksum, confirmation response, sequence number, timeout retransmission, connection management, flow control, congestion control, etc.
  • TCPSuitable for applications that require reliable transmission, such as file transfer

UDP

  • UDPIt is connectionless, that is, there is no need to establish a connection before sending data, and the data transmission load is relatively small.
  • UDPUse best-effort delivery, which does not guarantee reliable delivery, and does not use flow control and congestion control.
  • UDPThe first 8byte includes source port, destination port, length, checksum information.
  • UDPIt 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.
  • UDPIt 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 the IPlayer after the header is added .
  • UDPThe 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.
  • UDPOnly IPa few functions are added to the datagram service, that is, the function of the port and the function of error detection.
  • UDPSuitable 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 TCPreliable 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, UDTlike the use of open source UDPto achieve reliable data transmission, in addition there are incorporated TCPreliability and the UDPspeed tradeoff KCPprotocol.

The difference between TCP short connection and long connection

Short connection: Clientto Serversend a message, Serverrespond Client, and then write once completed, this time both sides can initiate any closeoperation, but these are generally Clientthe first to initiate closethe operation. Short connections generally only Client/Serverpass 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 TCPwaste time and bandwidth on establishing and closing operations.
Long connection: Clientto 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 TCPoperation 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 Serverload 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 LRUalgorithm, limit the number of client links, etc.

TCP sticky packet unpacking solution

Since TCPit 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 0filled), 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 FTPprotocol.
  • 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

Guess you like

Origin www.cnblogs.com/WindrunnerMax/p/12723155.html