1. Overview

The Transmission Control Protocol (TCP) is a connection-oriented protocol used by the Internet Protocol (IP) transport layer. Using a network connection, TCP allows a server and client pairs to exchange messages by using data segments packaged inside of data requests and responses.

In this tutorial, we’ll learn how to initiate this connection by sending a Synchronize Sequence Numbers (SYN) packet.

2. TCP

TCP provides a reliable connection for transferring data. Additionally, TCP has built-in error checking and guarantees the delivery of all data transmitted via its retransmission and acknowledgment processes. As a result, TCP is one of the most popular protocols in use on the internet.

The protocol requires a connection. It is the responsibility of the client to make the initial request to start the connection. The control bits provided in the TCP protocol header facilitate this request to connect.

3. Control Bits

As a rule, every TCP request and response packet starts with a header segment that contains critical information like Source Port, Destination Port, Packet Sequence Number, Packet Acknowledgement Number, Data Offset, Window Size, Checksum, and Flags. Our TCP client needs to make a connection, and with this purpose in mind, we’ll utilize the flags available to us in the packet header:

tcp header segment

Specifically, we’ll need the SYN flag and ACK flag. These flags are bits that are in the header, set to on with the value of 1 or off with the value 0. Ultimately, flags are how TCP clients and servers determine what to do with a packet they have received and how to respond. So let’s see how we use the SYN flag to make our connection.

4. 3-Way Handshake

Whenever a TCP conversation needs to occur, the client initiates and tries to make a connection. The server is passively open and always listening for connections. A packet sent to the server by the client begins an active open handshake. This is the 3-way handshake, named as such because 3 steps have to occur to bring up a connection. Furthermore, each of these steps requires specific flag settings. So, let’s review each step:

3 way handshake

4.1. SYN

First, the client sends a packet with a sequence number and only the SYN flag bit set in the header. This initial packet allows the client to set what the first sequence number should be for request packets originating from the client. This is the client’s synchronization step.

4.2. SYN/ACK

Second, the server responds to the SYN packet with an SYN/ACK packet. Here, the server sets both the SYN flag bit and the ACK flag bit. This packet confirms the sequence number sent by the client by acknowledging it. However, the server must also send an SYN and a sequence number back to the client to set what the first sequence number should be for response packets originating from the server. Similar to the first step, this response packet is the server’s synchronization step.

4.3. ACK

Finally, the client responds to the SYN/ACK packet with an ACK packet that acknowledges the server’s sequence number request.

At this point, the 3-way handshake is complete, the sequence numbering is in synch, and the connection is up and available for data to be sent to the server. Additionally, using the SYN bit/packet again will most likely cause a protocol error resulting in the termination of the connection.

5. Conclusion

In this tutorial, we learned how to create a TCP connection between a client and server by sending the correct packets to initiate and complete the TCP 3-way handshake.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.