Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Shaking hands three times and waving hands four times, it turns out TCP is so polite!

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > IT Information >

Share

Shulou(Shulou.com)11/24 Report--

We need to follow certain rules when talking to others, such as making sure that they understand our language. If you speak Chinese to a foreigner who does not understand Chinese, it is impossible to communicate well.

The same is true of communication between computers, which must follow certain rules in order to "communicate" smoothly.

TCP and UDP are two protocols in the TCP / IP protocol stack (Internet protocol series). TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are two protocols in the transport layer. We usually brush videos, play games, watch news, etc. through these two protocols for data transmission.

So what's the difference between these two protocols?

TCP UDP Difference Connection-Oriented

reliable

provide flow control

Support one-to-one communication only

non-connection oriented

unreliable

No flow control provided

Support one-to-one, one-to-many, many-to-one, many-to-many communications

Advantage transmission data safe and reliable transmission speed, occupy less resources After reading the above table, is it still a little confused, the following small series uses a metaphor to tell you what the difference is between the two.

We think of communication between two applications as two people communicating. Regardless of time, we think of TCP as a phone call and UDP as a letter.

When two people call, they need to dial the other party's phone in advance, which is the need to establish a connection; during the call, both parties can confirm the message in time, and if they cannot hear clearly, they can ask the other party to say it again, which is safe and reliable.

To write a letter, you only need to send the letter according to the address, which means that you don't need to establish a connection; the letter sent doesn't know whether the other party can receive it, which is unreliable.

In order to ensure the integrity of the transmission file, TCP will control the sending rate of the sender according to the receiving rate of the receiver, that is, implement traffic control, so TCP transmission speed is lower than UDP.

These two protocols do not exist which is good or which is bad, both have their own suitable application scenarios.

For example, when transmitting files, there is no requirement for speed, but it must be ensured that the files are delivered completely and there is no data loss. At this time, TCP protocol should be used, and when we are in video chat, the timeliness requirements are high and the accuracy requirements are slightly lower.

TCP is a connection-oriented protocol, just as you need to dial the phone ahead of time to make a call and hang up after ending the call, so how does TCP establish and disconnect connections?

The same is true of communication between computers, which must follow certain rules in order to "communicate" smoothly.

TCP three-way handshake Before transmitting data, two hosts need to establish a connection through three sessions, a process we call a three-way handshake.

First handshake: client requests connection to server,

SYN=1 (connection established),

seq=x (serial number),

The client enters the SYN_SENT state.

Second handshake: the server returns an acknowledgement to the client and requests to establish a connection,

SYN=1 (connection established),

ACK=1 (received),

ack=x+1 (acknowledgement number is received serial number plus one),

seq=y (serial number),

The server enters SYN_RCVD state.

Third handshake: client sends acknowledgement message to server,

ACK=1 (received),

ack=y+1 (acknowledgement number is received serial number plus one),

seq=x+1 (serial number),

After the three-way handshake is completed, data can be transmitted between the two hosts ~

TCP's four waves When the data transfer is complete, the two hosts need to disconnect through four sessions, a process we call four waves.

First wave: client requests disconnection from server,

FIN=1 (disconnected),

seq=u (serial number),

The client enters the FIN_WAIT_1 state.

second wave: the server returns an acknowledgement message to the client,

ACK=1 (received),

ack=u+1 (acknowledgement number is received serial number plus one),

seq=v (serial number),

The server enters the CLOSE_WAIT state and the client enters the FIN_WAIT_2 state.

Third wave: After the server completes the data transmission, it sends a disconnection request to the client,

FIN=1 (disconnected),

ACK=1 (received),

ack=u+1 (acknowledgement number is received serial number plus one),

seq=w (serial number),

The server enters the LAST_ACK state.

Fourth wave: client returns acknowledgement message to server,

ACK=1 (received),

ack=w+1 (acknowledgement number is received serial number plus one),

seq=u+1 (serial number),

The client enters the TIME_WAIT state and the server enters the CLOSED state.

When the client is in TIME_WAIT state, the TCP connection has not been released, and after waiting for 2 MSL (Maximum Segment Lifetime), the client enters CLOSE state.

See here, I believe everyone still has some questions, the following will be answered by Xiaobian one by one.

Why not a three-way handshake, a two-way handshake or a four-way handshake? If it is a two-way handshake, the following situation may occur.

The client sends a request to establish a connection, but the response is delayed due to network congestion. The client sends a connection request again, the server responds, and the connection is established.

After a period of time, the first connection request sent by the client arrives at the server. The server thinks that the client rerequests to establish a connection (in fact, there is no connection). At this time, the server will return a response message and is always in a state of waiting for connection. This causes resource waste, as shown in the following figure.

Why not a four-way handshake?

The four-way handshake can also achieve the effect of a three-way handshake, that is, the original second handshake is split into two, one sending an acknowledgement message and one sending a request to establish a connection separately, but this also causes a waste of resources, as shown in the figure below. So finally decided to establish a connection through a three-way handshake.

Why four waves? Three waves? You can't. When the client stops sending data after sending the disconnection request (the client can still receive data), it is possible that the server still has data to send to the client at this time, so it first returns an acknowledgement message, and after sending all the data, it sends a disconnection message to inform the client that the connection can be disconnected.

After four waves, why didn't the client shut down immediately? The client does not shut down immediately, but enters the TIME_WAIT state and waits 2 MSL before entering the CLOSE state, which is to ensure that the fourth wave acknowledgement message reaches the server.

If the server does not receive the last confirmation message within the specified time, it will resend the connection for the third time, and the client will resend the confirmation message, as shown in the following figure.

MSL is the longest survival time of the message, 2 MSL is the longest time required for two messages to go back and forth in the network. If the client does not receive the disconnection request again after this time, it means that the four waves are successfully completed and the connection can be disconnected.

Summarize today's content here, review today's content together with Xiaobian:

TCP and UDP are two protocols in the transport layer, TCP is safe and reliable but slow, UDP is fast but may lose data, these two protocols have their own advantages, suitable for different application scenarios.

The process of connecting and disconnecting two hosts is known as a "three-way handshake" and a "four-way wave."

Understand why it must be three handshakes and four waves.

I believe that through today's study, when asked about TCP and UDP in the future, everyone will be able to talk freely.

This article comes from Weixin Official Accounts: ZTE Document (ID: ztedoc)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

IT Information

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report