TCP Handshake (part-2)

30.06.2024

cover

Next, let’s analyze the handshake. To do this, we will need Wireshark (to listen to TCP packets and communication, of course). Now, let’s start the server and observe what happens using the Wireshark interface.

When you click the button on the left side of the red rectangle, it starts listening to the packets. After entering the program, it will ask you where you want to listen to the incoming and outgoing communication. You can select the one with the label Loopback:

Nothing happened yet. Now, let's start the client side:

Wow, progress! This means a two-way communication has been established, and data has been exchanged.

The First SYN

Nothing happened while the server was running, but as soon as the client started, it sent the first SYN packet to the server.

If you look at the panel at the bottom left, you’ll notice fields such as Acknowledgment Number, Flags, Sequence Number, Window size, and others.

  • Acknowledgment Number: A unique identifier sent by the receiving device to confirm the successful receipt of data packets.
  • Flags: Used to identify the type of the TCP packet sent.
  • Sequence Number: Ensures that data packets are sent in the correct order and that none are missed.
  • Window size: Limits the amount of data the server can send to the client without waiting for confirmation. (This is typically 65,535 bytes, meaning you can’t send more than that in one transfer.)

All of these are managed by the framework or library you’re using, so you don’t need to worry about them. Thus, the first SYN packet sent by the client to the server can be considered a "greeting" or "introduction" packet.

Response from the Server

The server responds to this SYN with a SYN-ACK packet. If you look at the above image, you’ll see the Info section of the second packet shows SYN-ACK (which are examples of the flags mentioned earlier).

ACK Response from the Client

This is the final part of the handshake (the handshake). Here, the client notifies the server of its current sequence number. This sequence number will be 1 more than the previous sequence number (like how 1 comes after 0). It also includes the expected sequence number from the server, which is the acknowledgment number.

Conclusion

As you can see, if you start the client-server again, they will exchange nearly 10 packets in the same way. I hope this post helps explain why opening connections such as database connections, TCP connections, websockets, and others is not always a good practice.

Take the time to deeply understand the language, framework, or technology you are learning. This way, you will have a clear understanding of when each method, function, or class opens a connection and when it closes.

Feel free to share the post,

You can also bring me a coffee (you’ll find more information in the channel profile).