TCP (Transmission Control Protocol)

The characteristics of TCP

1. TCP is a connection-oriented transport layer protocol

2. Every TCP connection has two endpoints, that is, point-to-point

TCP provides reliable delivery, that is, reliable connections

4. TCP provides full-duplex communication.

5. TCP byte stream oriented

The header format of the TCP packet segment

The meanings of the fixed format fields in the header are as follows:

1. Source port and destination port

The source port number and destination port number are written in two bytes each.

2, the serial number

At four bytes, TCP is byte stream oriented, and each byte in the byte stream transmitted over a TCP connection is numbered sequentially.

3. Confirmation Number

It is the serial number of the first data byte of the next packet segment expected to be received from the other party.

4. Data offset

It is 4 bytes. It indicates how far the start of the TCP packet is from the start of the TCP packet. The header length of the TCP packet segment is specified.

5, retention,

It takes 6 bytes and is reserved for future use, but should be set to 0 for now.

6,

Emergency URG: Emergency flag. When URG=1, the emergency pointer field is valid. It tells the system that urgent data in this message segment should be transmitted as soon as possible. Equivalent to high-priority data. The sender then inserts the emergency data in front of the column data, in conjunction with the emergency pointer field at the head

Acknowledgement ACK: Indicates the acknowledgement flag. When ACK=1, the confirmation number field is valid; when ACK=0, the confirmation number is invalid. According to TCP, ACK must be set to 1 for all packet segments transmitted after a connection is established.

Push PSH: Push flag. When PSH is 1, the receiving end may deliver data to the receiving application process, that is, push data forward.

Reset RST: indicates the reset flag. When RST is 1, the TCP connection is reset.

SYN: Indicates the SYN flag. When SYN=1 and ACK=0, it indicates that this is a connection request packet segment. If the peer agrees to the connection, set SYN=1 and ACK=1 in the response packet segment. Therefore, setting SYN to 1 indicates that this is a connection request or a connection receiving packet.

Terminate FIN: Terminate flag. FIN: releases a connection. If FIN=1, the sender of the packet has sent all data and wants to release the transport connection.

7, window,

The 2-byte window refers to the receiving window of the party that sent the message. The window value serves as a basis for the receiver to let the sender set its sending window. Indicates the amount of data allowed to be sent.

8. Check and

It is 2 bytes, and the orientation of the check and field check includes the head and data.

9. Emergency Pointers

The emergency pointer is only meaningful if URG=1. It indicates the number of bytes of emergency data in this paragraph.

10 and options

The length is variable, up to 40 bytes. When no option is used, the TCP header length is 20 bytes.

11, filling

The fields are populated only so that the entire TCP header length is a multiple of 4 bytes.

12, the data

The data carried in the message.

TCP three-way handshake

The three-way handshake is to establish a TCP connection. To establish a TCP connection, the client and server send a total of three packets to confirm the connection.

For the first handshake, the Client sets the SYN flag bit to 1, randomly generates a value of SEq =J, and sends the packet to the Server. The Client enters the SYN_SENT state and waits for confirmation from the Server.

For the second handshake, after receiving the packet, the Server realizes that the Client requests to establish a connection by flag bit SYN=1. The Server sets flag bit SYN and ACK to 1, ACK=J+1, randomly generates a value seq=K, and sends the packet to the Client to confirm the connection request. The Server enters the SYN_RCVD state.

After receiving the handshake for the third time, the Client checks whether the ACK is J+1 and ACK is 1. If the ack is correct, the Client sets the flag ACK bit to 1 and ack=K+1 and sends the packet to the Server. The Server checks whether the ACK is K+1 and ACK is 1. The Client and Server enter the ESTABLISHED state and complete the three-way handshake. Data can be transmitted between the Client and Server.

TCP wave four times (important!!)

The so-called quadruple wave is to disconnect a TCP connection. To disconnect a TCP connection, the client and server need to send a total of four packets to confirm the connection.

The TCP connection is in full duplex, so each direction must be closed separately. This principle is that when a party finishes sending data, it sends a FIN to terminate the connection in this direction. Receiving a FIN only means that no data flows in this direction, that is, no more data is received. However, data can still be sent on this TCP connection until a FIN is also sent in that direction. The party that closes first performs an active shutdown, while the other party performs a passive shutdown, as depicted in the figure above.

On the first wave, the Client sends a FIN to disable data transfer from the Client to the Server, and the Client enters the FIN_WAIT_1 state.

For the second wave, the Server sends an ACK to the Client after receiving the FIN. The ACK sequence number is +1 (the same as that for SYN, one FIN occupies one sequence number). The Server enters CLOSE_WAIT state

After the third wave, the Server sends a FIN to disable data transfer from the Server to the Client, and the Server enters the LAST_ACK state.

After receiving the FIN for the fourth time, the Client enters the TIME_WAIT state and sends an ACK to the Server to confirm that the FIN number is +1. The Server enters the CLOSED state and waves the FIN for four times.

Why is it three handshakes to establish a connection, but four waves to close it? (Send ACK and FIN separately)

This is because in LISTEN state, the server receives a SYN packet for establishing a connection and sends the ACK and SYN packets to the client. And close the connection, when I received the other side of the FIN message just said to each other can no longer send data but also receives the data, the board may not all data are sent to each other, so their can immediately close, also can send some data to each other, then send the FIN message now agreed to close the connection to the other side, therefore, Your ACK and FIN are usually sent separately.

UDP (User Datagram Protocol)

The characteristics of UDP

1, UDP isNo connection, that is, there is no need to establish a connection before sending data, thus reducing the overhead and delay before sending data.

2. UDP uses best effort delivery, that is, reliable delivery is not guaranteed, so hosts do not need to maintain complex connection status tables.

3. UDP is packet oriented. The UDP delivered by the sender to the application is delivered down to the IP layer after the header is added.

4. UDP does not have congestion control, so network congestion does not reduce the sending rate of the source host.

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

6, UDP header overhead is small, only 8 bytes, shorter than TCP 20 bytes header.

UDP header format

1. Source port

Source port number. When you need a reply, you can set it to all 0 when you don’t need it.

2. Destination port

Destination port number. This must be used when the endpoint delivers the message.

3, length,

The length of a UDP user datagram. The minimum value is 8 (header only).

4. Check and

Detects UDP user datagrams for errors in transmission. If it’s wrong, throw it away.

UDP test and

UDP header verification and calculation method is a bit special. When calculating the checksum, a 12-byte dummy header is added before the UDP user packet. Note that this dummy header is only added temporarily to the UDP user datagram. The dummy header is neither sent down nor sent up, but is only used to compute the sum.

UDP computes the sum in a similar way to the IP datagram header sum, except that the IP packet is checked and only the IP packet header is checked, while UDP computes both the header and the data part.

Differences between TCP and UDP

References for this article

TCP protocol details

Xie Xiren. Computer Networks. 6th edition