01 logo

Explain the principles and differences of TCP and UDP protocols with pictures and texts

Hacker technology

By Nell JonasPublished 2 years ago 6 min read
Like

First of all, both tcp and udp work at the transport layer and are used to transfer data between programs. Data generally contains: file type, video type, jpg pictures and so on.

II. Differences

TCP is connection-based, while UDP is connection-based.

Tcp transmission data is stable and reliable, suitable for scenarios that require high quality of network communication, and need to be accurately transmitted to each other, such as transferring files, sending e-mail, browsing web pages and so on.

Udp has the advantage of high speed, but it may cause packet loss, so it is suitable for scenarios that require high real-time performance but do not require a small amount of packet loss. For example: domain name query, voice call, live video and so on. Another very important application scenario for udp is tunnel network, such as VXLAN

Take the communication between people as an example: the UDP agreement is equivalent to writing to the other party. After sending the letter, you can't know whether the other party has received the letter, whether the content of the letter is complete, and can not get timely feedback, while the TCP protocol is like a telephone communication. In this series of processes, you can get timely feedback and ensure that the other party receives it in a timely manner. As shown below:

3. The process of TCP communication:

How does tcp guarantee the above process?

It is divided into three steps: three-way handshake, transmission confirmation, and four waves. The three-way handshake is the process of establishing a connection.

Four, three handshakes:

When the client initiates a connection to the server, it will first send a packet of connection request data to ask if a connection can be established with you. This packet of data is called SYN packet. If the peer agrees to connect, it replies to a packet of SYN+ACK packet. After receiving it, the client sends a packet of ACK packet, and the connection is established. Because three packets of data are sent to each other in this process, it is called a three-way handshake.

Why three handshakes instead of two handshakes?

This is to prevent, because the invalid request message, suddenly sent to the server, causing an error, what does this mean?

Suppose the client uses two handshakes to establish a connection, and the client sends a syn packet to the server to request the connection. Due to some unknown reasons, it does not reach the server, and a network node is stranded in the middle. In order to establish the connection, the client will resend the syn packet. This time the data packet is delivered normally, and the connection is established after the server sends syn+ack.

However, the network blocked by the first packet of data suddenly resumes, and the first packet of syn packet is sent to the server. At this time, the server will think that the client has initiated a new connection, thus entering the waiting state of data after two handshakes. The server thinks that it is two connections, while the client thinks it is one connection, resulting in inconsistent state. If in the case of three-way handshake, the server cannot receive the final ack packet. Naturally, you don't think the connection was successful.

So the three-way handshake is essentially to solve the problem of unreliable network channel, in order to establish a reliable connection on the unreliable channel, after three-way handshake, both the client and the server enter the state of data transmission.

V. data transmission

Data transfer:

A packet of data may be split into multiple packets to send, how to deal with the problem of packet loss, these packets arrive in different order, how to deal with the problem of disorder?

To solve these problems, the tcp protocol establishes a sending buffer for each connection. The sequence number of the first byte after establishing the link is 0, and the sequence number of each byte is increased by 1. When sending data, a part of the data is taken from the data buffer to form a sending message, and the sequence number and length are attached in the tcp protocol header. The receiver needs to reply to the confirmation message after receiving the data. The ack in the confirmation message is equal to accepting the sequence number plus the length, that is, the starting sequence number of the next packet data transmission. In this question-and-answer transmission mode, the sender can confirm that the data sent has been received by the other party, and the sender can also send continuous multi-packet data at one time, and the receiver only needs to reply to ack once. As shown in the figure:

Six or four waves:

Both the client and the server in the connected state can initiate a request to close the connection, and you need to wave four times to close the connection. Suppose the client initiates a connection closing request, he initiates a FIN packet to the server, indicating that the connection is to be closed, he enters the termination waiting for 1 loading, the server receives the FIN packet and sends an ACK packet, indicating that he has entered the closed waiting state, and the client enters the termination waiting 2 state. this is the second wave, and the server can send unsent data at this time, and the client can also accept the data. After the server sends the data, it sends a packet of FIN packet and finally enters the confirmation status. This is the third time to wave. After receiving the ACK packet, the client resumes the ACK packet, enters the timeout waiting state, closes the connection after the timeout, and closes the connection immediately after the server receives the ACK packet. This is the fourth time to wave.

Why does the client wait for the timeout? This is to ensure that the other party has received the ACK packet, because assuming that the client releases the connection after sending the last ACK packet, once the ACK packet is lost in the network, the server will remain in the final acknowledgement state. If you wait for a period of time, the server will resend the FIN packet because it does not receive the ack packet, and the client will resend the ack packet in response to the FIN packet and refresh the timeout This mechanism is the same as the third handshake. It is also to ensure reliable confirmation of disconnection in unreliable network links.

VII. UDP agreement

Udp: first of all, the udp protocol is disconnected. To send data is to encapsulate a simple packet and then send it from the network card. There is no state connection between the packets. Because of the simple processing method of udp, its performance loss is very small, and the memory resources of cpu are much less than that of tcp, but udp cannot guarantee the packet loss during network transmission. Therefore, udp is weaker than tcp in terms of transmission stability.

Therefore, the main difference between tcp and udp: tcp data transmission is stable and reliable, suitable for scenarios that require high quality of network communication, and need to be transmitted to each other accurately. For example, transfer files, send e-mail, browse the web, etc., udp has the advantage of high speed, but may cause packet loss, so it is suitable for scenarios with high real-time requirements but not too large requirements for a small amount of packet loss. For example: domain name query, voice call, live video and so on.

Another very important application scenario of udp is tunnel network, such as VXLAN.

hackers
Like

About the Creator

Nell Jonas

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.