TCP control (three-way handshake)

TCP control (three-way handshake)

The three-way handshake is also known as the 3-way handshake or “three-way handshake”.

 When a TCP connection is opened, a connection is established using a procedure called a three-way handshake. During this handshake, initial sequence numbers are exchanged and the two hosts synchronize their initial sequence numbers.

Exchanging sequence numbers is important in TCP communication.

 The data to be sent is divided into transmission units called segments. Also, IP communication does not guarantee that packets will arrive at the other party in the order they were sent. Packets can also arrive in pieces.

Therefore, TCP uses a sequence number so that the transmitted segment can be restored to the original data.

 The sequence number points to each byte position in the transmitted data, and it is possible to restore the original data by arranging it based on the number.

sequence number

A TCP header is constructed as follows:

TCP control (three-way handshake)

As you can see from the figure above, the sequence number is 32 bits long. It is represented as an unsigned integer value.

 In TCP, when a connection is started, each channel is given a random sequence number as an initial value.

 The reason why it is random is that it is easy to disguise the segments if they always start with a fixed number or are assigned in order such as “1, 2, 3, 4, 5 …”. Therefore, in the three-way handshake, the initial value is set to a value randomly determined at the time of connection.

Three-way handshake procedure

 In TCP communication, when opening, a connection is established by a procedure called three-way handshake. In this handshake, the two hosts synchronize their initial sequence numbers by exchanging initial sequence numbers.

Synchronization is done by exchanging packets consisting of a SYN (synchronization control bit) and an initial sequence number.

The steps are as follows:

procedurecontent
Step 1The sender sends a “SYN packet” to the other party.
Step 2The other party that receives the SYN packet sends a “SYN, ACK packet” to allow the connection to the sender.
Step 3The sender that receives the SYN ACK packet sends an ACK packet indicating connection initiation and initiates communication with the receiver.

A diagram of the three-way handshake is shown below.

The flow in the above figure is as follows.

1. sender → receiver

The sender sends a SYN packet to the receiver indicating that the initial sequence number is ‘x’.

From sender to receiver

“SYN,sequence number of sender=x”

is sent.

2. Sender ← Receiver

 The receiver records that the sender’s sequence number is ‘x’ and sends a packet to the sender with its initial sequence number set to ‘y’ and ACK set to ‘x+1’.

Having “ACK=x+1” means that up to “x” octets have been received, followed by “x+1”.

From receiver to sender

“SYN, sequence number of receiver = y, ACK = x+1”

is sent.

3. sender → receiver

 The sender records that the receiver’s sequence number is “y” and sends a packet with ACK set to “y+1” to the receiver.

 ”ACK=y+1″ means that octets up to “y” have been received, and then “y+1” is expected.

From sender to receiver

“ACK=y+1”

is sent.

In this way, the three-way handshake establishes a connection by sending three packets back and forth.

You can see that TCP has much more complicated control than UDP.

To make it a little easier to understand, this time we will specifically show the exchange when the initial sequence number is the following value.

Initial sequence number of sender: 1000 Initial sequence number
of receiver: 2000