Location>code7788 >text

TCP and UDP protocols

Popularity:419 ℃/2024-10-16 10:23:39

Since you can find this document that you are still a network of white, this piece of paper will do a basic introduction to TCP and UDP, the advantages and disadvantages of comparison, as well as applicable scenarios believe that after reading this you will have a full understanding of TCP and UDP.

Introduction to TCP and UDP

If the network model is simply divided into four layers, the application layer first gives the data to the transport layer, and the transmission of the transport layer is based on the network cable or other media to complete, the transport layer actually provides a logical communication.
TCP and UDP are the transport layers!
The transmission of TCP and UDP protocols can then be understood as the application layer data arriving at the destination in different ways.TCP and UDP are the two different ways.

Difference between TCP and UDP

For data transmission:
TCP transferring dataIt won't be sent immediately.Instead, there will be three handshakes and a successful handshake before the data is sent. (I won't go into the specifics of how the handshakes are done here.)
The purpose of the handshake is to perform a one-to-one service. In this way the data can be thought of as being intransmission under reliable logical communications.

UDP does not perform a handshake, butSending data directly*, i.e., without a connection, is a more efficient way of sending data than TCP.More flexible he can communicate not only one-to-one but also one-to-many. *

Some students may have questions about the above description
That's why UDP doesn't even need to send data without handshaking why keep UDP, can't you just jump to the transport layer?
Reason: Both UDP and TCP provide ports, and without ports applications have problems trying to communicate at the same time. So you can't just ditch UDP and jump to the network layer!

Data header content aspects:
Both UDP and TCP have ports at the beginning, but they are very different. Of course, as you can see from the picture below, UDP is lighter and faster, while TCP has a higher overhead.

  • TCP
    From the picture above, we can also see that TCP has "sequence number" and "OK promise number" which UCP does not have. After the application layer gives the data to TCP, if the data is large, TCP will split it into multiple data segments and label each segment with a sequence number.
    However, only the "serial number" is meaningless! Only the "sequence number" is meaningless, but also need to confirm whether the data has reached the destination, so also need to "confirm the promise number". If the receiver receives this data segment, it can use the sum of the sequence number + the length of the data as the confirmation number to return.
    For the sender it's simple, not receiving an acknowledgement number within the specified time means that this data may have lost a packet.

-UDP
But the UDP prefix doesn't have either of those things, not the "sequence number" and the "OK promise number."
UDP does not have a "sequence number", the most direct feeling is that there is no order, everything is chaotic.
What UDP can do is treat each message coming down from the application layer as a separate message so that UDP is not fragmented and encapsulated into data segments at the transport layer and given directly to the network layer so that there is a clear boundary between the data segments.
Hence the argument for sending it line by line. Of course this approach is likely to be disorganized (and of course it's possible to have the application layer give the disorganized UDP a tidy up)

UDP does not have a "confirmation of the promise" so that the most intuitive feeling is that there is no bottom of the results of the transmission (data arrival, whether it is lost in the process of sending).
But it doesn't matter! It can be resent, and this method of transmission is better than the overhead and speed of TCP even if it's only conveyed successfully the third time!

In order to ensure data transmission reliability and network efficiency.
-TCP
TCP in order to ensure that the transmission rate in the network can adapt to the network conditions, to avoid data loss and overload, the introduction of flow control and congestion control.
Flow control: is for both senders. I.e., let the sender slow down and not send too much data at once, because the receiver doesn't have the means to handle so much data at this time.
Congestion control: is for the middle of the network between the sending parties, TCP can be more effective use of the network's bandwidth, after all, the entire Internet is used by everyone together, can not be originally network is very congested under the premise of the data sent desperately.

-UDP
UDP has no congestion control or flow controlIf the receiver can't receive so much data or the network is already congested, UDP will still be able to send it.
So operators will control at different levels . For example, carriers will put some limits on UDP traffic.

Performance overhead:

-TCP
The TCP header contains at least 20 bytes of information, plus additional options and padding. In addition TCP is a connection-oriented protocol that requires a connection to be established before the data is transferred (three handshakes) and a connection to be released after the data transfer is complete (four handshakes) So since TCP requires a connection to be established and data integrity to be guaranteed, TCP overhead will be greater than UDP.

-UDP
Although UDP is connectionless and does not guarantee data reliability, connectionlessness and low initial overhead are advantages for some protocols.
Such as: DNS core only in the query does not require a large amount of data, to be the results and efficiency, while UDP in the small amount of data can be simple and efficient
Such as: real-time voice and video is also very suitable for UDP, if you do real-time video because of packet loss may be missing images, but overall does not affect the use of still can.

Assumptions made to help better understand TCP and UDP

To help with understanding here is an assumption:
1, assuming that if the real-time data using TCP, TCP may be due to packet loss and retransmission, has arrived in the need to wait for those who did not arrive, which creates a time difference in the present data may become the past data.

2, assuming that the use of UDP to send a document, if the normal send success is okay, but in the middle of the packet loss there will be missing data, which will result in ambiguity of the content, and therefore are considered to use TCP to send (TCP will establish a connection to ensure the reliability of the data)

Applicable Scenarios

TCP is suitable for web pages, transferring files, e-mail sending, and other situations where you need to maintain a connection for a long period of time.
UDP is suitable for real-time audio and video transmission, online gaming, etc. These application scenarios require extremely high real-time data availability, while slight data loss is acceptable relative to transmission delay.

To summarize: UDP does not guarantee the reliability of data delivery, but is sometimes better suited to targeted scenarios than TCP.
1, connection mode: TCP is a connection-oriented protocol, UDP is a connectionless protocol.
2, reliability: TCP provides reliable transmission, to ensure data integrity and order, while UDP does not guarantee data integrity and order.
3, speed: UDP is faster than TCP because it does not need to establish a connection and maintain the connection status.
4, the transmission mode: TCP is based on the byte stream + transmission mode, UDP is based on the datagram transmission mode
5, performance overhead: TCP overhead than UDP, TCP header needs 20 bytes, UDP header as long as 8 bytes
TCP is unbounded with congestion control and UDP is bounded without congestion control.