TCP\UDP\TCP three-way handshake and four-way wave

2023.10.25

TCP\UDP\TCP three-way handshake and four-way wave


TCP is a connection-oriented, reliable, byte stream-based transport layer communication protocol.

What is TCP?

TCP is a connection-oriented, reliable, byte stream-based transport layer communication protocol.

  • Connection-oriented: The connection must be "one-to-one". It cannot be like the UDP protocol, which allows one host to send messages to multiple hosts at the same time. That is, one-to-many cannot be achieved;
  • Reliable: No matter what link changes occur in the network link, TCP can guarantee that a message can reach the receiving end;
  • Byte stream: When user messages are transmitted through the TCP protocol, the messages may be "grouped" into multiple TCP messages by the operating system. If the receiving program does not know the "message boundaries", it cannot read a valid message. of user messages. And TCP messages are "ordered". When the "previous" TCP message is not received, even if it receives the subsequent TCP message first, it cannot be thrown to the application layer for processing. At the same time, "repeated" ” TCP packets will be automatically discarded.

The difference between UDP and TCP

1. Connect

  • TCP is a connection-oriented transport layer protocol. A connection must be established before data can be transmitted.
  • UDP does not require a connection and transmits data immediately.

2. Service objects

  • TCP is a one-to-one two-point service, that is, a connection has only two endpoints.
  • UDP supports one-to-one, one-to-many, and many-to-many interactive communication

3. Reliability

  • TCP delivers data reliably, with no errors, no loss, no duplication, and data arriving on demand.
  • UDP is a best effort delivery and does not guarantee reliable delivery of data.

4. Congestion control, flow control

  • TCP has congestion control and flow control mechanisms to ensure the security of data transmission.
  • UDP does not. Even if the network is very congested, it will not affect the sending rate of UDP.

5. Initial overhead

  • The length of the TCP header is long and there will be a certain overhead. The header is 20 bytes when the "option" field is not used. If the "option" field is used, the header will become longer.
  • The UDP header is only 8 bytes and is fixed, so the overhead is small.

6. Transmission method

  • TCP is streaming, without boundaries, but guaranteed to be sequential and reliable.
  • UDP is sent packet by packet and has boundaries, but packet loss and disorder may occur.

7. Different shards

  • If the TCP data size is larger than the MSS size, it will be fragmented at the transport layer. After the target host receives it, it will also assemble the TCP data packet at the transport layer. If a fragment is lost midway, it only needs to transmit the lost fragment. .
  • If the UDP data size is larger than the MTU size, it will be fragmented at the IP layer. After the target host receives it, it assembles the data at the IP layer and then transmits it to the transport layer.

TCP and UDP application scenarios:

Since TCP is connection-oriented and can ensure reliable delivery of data, it is often used for:

  • FTP file transfer;
  • HTTP / HTTPS;

Because UDP is connectionless-oriented, it can send data at any time, and the processing of UDP itself is simple and efficient, so it is often used for:

  • Communication with a small total number of packets, such as DNS, SNMP, etc.;
  • Video, audio and other multimedia communications;
  • broadcast communications;

TCP three-way handshake and four-way wave

The TCP three-way handshake and four-way wave are also popular test points for interview questions. They correspond to the TCP connection and release process respectively. Let’s briefly understand these two processes.

TCP three-way handshake

Before understanding the specific process, we need to understand several concepts first

  • SYN: Its full name is Synchronize Sequence Numbers, synchronization sequence number. It is the handshake signal used by TCP/IP when establishing a connection. When a TCP connection is established between a client and a server, a signal is sent first. When the client receives the SYN message, it generates a random value X in its own segment.
  • SYN-ACK: After receiving the SYN, the server opens the client connection and sends a SYN-ACK as a reply. The acknowledgment number is set to one more than the received sequence number, which is X + 1, and the sequence number chosen by the server for the packet is another random number Y.
  • ACK: Acknowledge character, confirmation character, indicating that the data sent has been confirmed to be received correctly. Finally, the client sends an ACK to the server. The sequence number is set to the received acknowledgment value, Y + 1.

If we use real life as an example, it would be

Xiao Ming - Client Xiao Hong - Server

  • Xiao Ming called Xiao Hong. After the call was connected, Xiao Ming said hello, can you hear me? This is equivalent to establishing a connection.
  • Xiaohong responded to Xiaoming, "Can you hear it? Can you hear what I said?" This is equivalent to a request for response.
  • After Xiao Ming heard Xiao Hong's response, OK, this is equivalent to connection confirmation. After this, Xiao Ming and Xiao Hong can talk/exchange information.

TCP four waves

Using four waves during the connection termination phase, each end of the connection will be terminated independently. Let's describe this process below.

  • First, the client application decides to terminate the connection (the server can also choose to disconnect) . This causes the client to send a FIN to the server and enter the FIN_WAIT_1 state. When the client is in the FIN_WAIT_1 state, it waits for an ACK response from the server.
  • Then in the second step, when the server receives the FIN message, the server will immediately send an ACK confirmation message to the client.
  • When the client receives the ACK response sent by the server, the client enters the FIN_WAIT_2 state and then waits for the FIN message from the server.
  • After the server sends the ACK confirmation message, it will send a FIN message to the client after a period of time (after it can be closed) to inform the client that it can be closed.
  • When the client receives the FIN message sent from the server, the client will change from the FIN_WAIT_2 state to the TIME_WAIT state. Clients in the TIME_WAIT state are allowed to resend ACKs to the server in order to prevent information loss. The time the client spends in the TIME_WAIT state depends on its implementation. After waiting for a period of time, the connection is closed and all resources on the client (including port numbers and buffer data) are released.

You can still use the call example above to describe it.

  • Xiao Ming said to Xiao Hong, I have finished saying everything and I am going to hang up.
  • Xiaohong said, Received, I still have something to say.
  • After a few seconds, Xiaohong also finished speaking. Xiaohong said, I’m done. You can hang up now.
  • After Xiao Ming received the message, he hung up the phone after waiting for a while.