Before introducing TCP and UDP, let’s look at the layered computer network model. OSI 7-layer network model and TCP/IP 4-layer conceptual model, as well as the corresponding network protocol

TCP: indicates the transport protocol TCP

UDP: user data protocol UDP

TCP

TCP service model includes connection-oriented service and reliable data transfer service. When an application invokes TCP as its transport protocol, the application gets both services from TCP.

  • Connection-oriented services: TCP lets clients and servers exchange transport layer control information before application-layer data packets start flowing. This handshake prompts the client and server to get ready. During the handshake phase a TCP connection is established between the sockets of two processes, and the process is duplex. That is, the processes of both sides can simultaneously send and receive packets on the connection. When the application ends the packet sending, the connection must be removed.

  • Reliable data transfer service: Communication processes can rely on TCP, error-free, to exchange all data in the proper order as one end of the application passes bytes into the socket. It can rely on TCP to interact the same byte stream to the receiving socket without byte loss or redundancy.

A TCP connection

The TCP connection is established to solve the following three problems:

  1. Each side should be able to determine the existence of the other

  2. Allow both parties to negotiate parameters (e.g., maximum window value,)

  3. Ability to allocate transportation entity resources.

TCP three-way handshake and four-way wave

In The fourth edition of Computer Network written by Xie Xiren, the purpose of “three-way handshake” is “to prevent the invalid connection request message segment from being suddenly transmitted to the server, resulting in errors”. In another classic book, Computer Networks (Andrew S.Tanenbaum, 4th Ed.), the purpose of the three-way handshake was to solve the problem of “delayed repetitive grouping in networks.” These two different statements actually illustrate the same point.

cQP1i9.png

  • First handshake: The connection is established. The client sends sync package (sync= X) to the server and enters the SYN_SENT state for confirmation by the server. SYN: indicates the Synchronize Sequence number. A packet segment with SYN=1 in the header and initial sequence number seq=x cannot carry data but consumes one sequence number.

  • Second handshake: After receiving a Syn packet from the client, the server must confirm that the client also sends a Syn packet (ACK = X +1). In this case, the server enters the SYN_RECV state. In the acknowledgement packet segment, SYN=1, ACK=1, ACK= X +1, and seq= Y

  • Third handshake: The client receives the SYN+ACK packet from the server and sends an ACK packet (ACK = Y +1). After the ACK packet is sent, the client and the server enter the ESTABLISHED state (TCP connection is successful) to complete the three-way handshake. The ACK segment is ACK=1, the ACK number is ACK= Y +1, and the sequence number is SEq = X +1 (the initial value is SEq = X, so the second packet segment needs +1). The ACK segment can carry data. If it does not carry data, it does not consume the sequence number.

Why does TCP use a three-way handshake instead of a two-way handshake

Now, there are two possible explanations for this. The first is the one that’s circulating on the Internet:

The three-way handshake performs two important functions, both by preparing the parties to send the data (both parties know that they are ready) and by allowing the parties to negotiate the initial serial number, which is sent and confirmed during the handshake.

Now instead of three handshakes requiring only two handshakes, deadlocks can occur. As an example, consider the communication between computers S and C. Suppose C sends A connection request packet to S, which receives the packet and sends an acknowledgement reply packet. Following the two-handshake protocol, S considers that the connection has been successfully established and can start sending packets of data. However, in the case that THE reply packet of S is lost in transmission, C will not know whether S is ready, do not know what sequence number S establishes, and C even doubts whether S has received its connection request packet. In this case, C considers that the connection has not been established successfully, and ignores any data sent by S, and only waits for the connection to confirm and reply the grouping. S repeatedly sends the same packet after the sent packet times out. This creates a deadlock

The second is an example from Xie xiren’s version of Computer Networks:

cQiMOf.png

Note: The yellow part of the figure above illustrates why the two-handshake is not allowed.

Why is TCP connected with a three-way handshake, but closed with a four-way handshake?

Let’s take a look at four waves to release the connection. The following is from Computer Networks (7th edition) edited by Xie Xiren

cQFu4J.png

cQFQ3R.png

So why do you need four handshakes to close?

After receiving a SYN request packet from the Client, the Server sends a SYN+ACK packet. ACK packets are used for reply, and SYN packets are used for synchronization. However, when the Server receives a FIN packet, the SOCKET may not be closed immediately. Therefore, the Server can only reply with an ACK packet to tell the Client, “I received the FIN packet you sent.” I can send FIN packets only after all packets on the Server are sent. Therefore, THE FIN packets cannot be sent together. Therefore, a four-step handshake is required.

What if a connection is established, but the client suddenly fails?

TCP also has a keepalive timer, so if the client fails, the server can’t wait forever, wasting resources. The server resets this timer every time it receives a request from the client, usually for two hours. If it does not receive any data from the client within two hours, the server sends a probe segment, which is then sent every 75 seconds. If there is no response after 10 probe packets are sent, the server assumes that the client is faulty and closes the connection.

UDP

UDP is a lightweight transport protocol that does not provide unnecessary services. It provides only minimal services. UDP is connectionless, so there is no handshake between two processes in communication. UDP provides an unreliable data transmission service. That is, when a process sends a packet into a UDP socket, UDP does not guarantee that the packet will reach the process, nor does it guarantee the order of the packet. UDP uses best effort delivery, UDP is message-oriented.

UDP does not include congestion control, so the sender of UDP can inject data to the lower layer (network layer) at any rate it chooses.

UDP supports one-to-one, one-to-many, many-to-one and many-to-many interactive communication

UDP has a small header overhead of 8 bytes, which is shorter than TCP’s 20 bytes header

cMokdS.png

TCP is different from UDP

  • TCP is connection-oriented; UDP is connectionless, that is, no connection is required before sending data.

  • TCP provides reliable services. Data transmitted through TCP connection, no error, no loss, no repetition, and in order to arrive; UDP ensures arrival as far as possible, not reliable interaction.

  • TCP is byte stream oriented. The data transmitted by actual TCP connection can be regarded as a series of unstructured byte stream. UDP is message-oriented and has no blocking control, so network congestion does not reduce the efficiency of the source host (useful for real-time applications).

  • Each TCP connection can only be point-to-point. UDP supports one-to-one, one-to-many, and many-to-many communication.

  • TCP header cost 20 bytes; The header of UDP has a small overhead of only 8 bytes

  • The logical communication channel of TCP is duplex reliable channel, while UDP is unreliable channel.

The above is a review of TCP and UDP knowledge concepts combined with computer network knowledge. If you like my content, please add attention and share.

Object oriented Coding

Personal Programming summary

47 original content

The public,