BFE.devAI解答 - 前端面试问题
172. What is the difference between TCP and UDP?
TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are two different transport layer protocols used in computer networks. Here are the main differences between TCP and UDP:
-
Connection-oriented vs. Connectionless: TCP is a connection-oriented protocol, which means it establishes a connection between the sender and receiver before transferring data. UDP, on the other hand, is a connectionless protocol, where data packets are sent without establishing a connection.
-
Reliability: TCP is a reliable protocol, as it ensures that all packets are received in the correct order by the receiver. If any packet is lost, TCP automatically requests retransmission. UDP, on the other hand, is unreliable, as it does not guarantee delivery or packet sequencing. It is up to the application layer to handle errors and ensure reliability.
-
Flow Control and Congestion Control: TCP has built-in flow control and congestion control mechanisms. It adjusts the transmission rate based on network conditions to avoid congestion and ensure efficient data transfer. UDP does not have these mechanisms, which means it can flood the network if sent at a fast rate.
-
Packet Header Size: TCP has a larger packet header size compared to UDP, as it includes various control information like sequence numbers, acknowledgment numbers, and window size. UDP's header is smaller, containing only the source and destination port numbers.
-
Usage: TCP is commonly used for applications that require reliable and ordered delivery of data, such as web browsing, email, file transfers, etc. UDP is used in scenarios where real-time communication or faster data transmission is required, like video streaming, online gaming, DNS, etc.
In summary, TCP provides reliable, ordered, and connection-oriented data transmission, while UDP offers faster, connectionless, and unreliable transmission. The choice between TCP and UDP depends on the specific requirements of the application and the trade-off between reliability and speed.