Meet these two guys first
Both TCP and UDP are protocols from the transport layer.
The transport layer is located on the second layer, as shown in the figure below.
The transport layer is located between the application layer and the network layer and is responsible for communication between processes located in different hosts.
One of my co-workers asked me: How do different computers communicate? Do you still need to develop the agreement yourself?
Analysis: This man feels that if he wants to develop an application, he needs to define his own protocol. In fact, it is not unreasonable. What is the agreement? An agreement is a rule that both parties communicate to abide by and is called an agreement. Like how to use HTTP, FTP, these are all application services, why is it called application services? Because it is an application dedicated to serving other people. Such as QQ, WeChat, Taobao, browser, etc. are usually called application clients, of course, we ca n’t see or touch these servers unless you are a developer involved. QQ uses the OICQ protocol, and HTTP services use the HTTP protocol. These protocols are definitely custom. If you use an HTTP server, you must obey this rule, otherwise, people will not recognize you. It is actually based on TCP or UDP.
The transport layer of the Internet provides two different services, and application developers must choose one of them.
It is a connection-oriented and reliable transport layer protocol.
It is a connectionless and unreliable transport layer protocol.
Where is reliable and unreliable?
Compared with IP, UDP only adds the process-to-process distribution service and error checking mechanism, because it also provides unreliable services.
And TCP provides services including reliable data transmission, flow control and congestion control for applications.
With reliable TCP, why we still need UDP?
Applications can control more granularly when and what data is sent
Send data without establishing a connection in advance
No connection status, simple implementation
Small head overhead
- The application can better control when and what kind of data is sent: without establishing a connection, UDP can send the message to the network layer as soon as possible; TCP may need to retransmit the segment that has not received the confirmation within the specified time. UDP does not have the delay introduced by establishing a connection, which is probably the most important reason why DNS chooses UDP instead of TCP.
- Simple implementation: Because UDP is connectionless, the Host does not need to maintain the connection status, and the implementation is simple;
- The header overhead is small: the UDP Segment header field has a total of 8 bytes, and the TCP header includes a total of 20 bytes.
UDP is used by most multimedia applications. but…
The advantages of TCP:
Reliable and stable.
The reliability of TCP is reflected in that TCP will have three handshakes to establish a connection before transmitting data, and there are acknowledgment, window, retransmission, and congestion control mechanisms during data transmission. After the data is transmitted, the connection will be disconnected to save system resources.
Disadvantages of TCP:
Slow, low efficiency, high resource consumption, and easy to be attacked; TCP must establish a connection before transmitting data, which will consume time, and in data transmission, confirmation mechanism, retransmission mechanism, congestion control mechanism, etc. will consume a lot of practice, And to maintain all transmission connections on each device. In fact, each connection will occupy the system’s CPU, memory and other hardware resources.
Because TCP has a confirmation mechanism and a three-way handshake mechanism, these advantages have become flaws, resulting in TCP being easily used by people to implement DOS, DDOS, CC, and guessing attacks.
Advantages of UDP:
Fast and slightly safer than TCP.
UDP does not have TCP handshake, confirmation, window, retransmission, congestion control. UDP is a stateless transmission protocol, so it is very fast when transferring data.
Without these TCP mechanisms (eg, three-way handshake), the vulnerability of attackers using UDP will be less. But UDP cannot avoid attacks, such as UDP Flood and so on.
Disadvantages of UDP:
Unreliable and unstable.
Because UDP does not have the reliable mechanisms of TCP, when data is transmitted, if the network quality is not good, it will be easy to lose packets.
When should the TCP transport protocol be used:
When there is a requirement for network communication, for example, the entire data should be passed to the other party accurately, which is often used for some reliable applications, such as HTTP, HTTPS, FTP, and other file transfer protocols, POP, SMTP, and other mail transfer protocols.
When should I use UDP:
When the quality of network communication is not high, the network communication speed is required to be as fast as possible, then UDP can be used. For example QQ language, QQ video, etc.
Interviews often ask: What is the difference between TCP and UDP?
- TCP connection-oriented, UDP connectionless;
- TCP occupies more system resources and less UDP;
- The TCP structure is complex, and the UDP twisting is simple;
- TCP is based on streaming mode and UDP datagram mode;
- TCP guarantees the correctness of data, UDP may lose packets;
- TCP guarantees data order, UDP does not guarantee.