TCP 3-Way Handshake…

Mahinsa Bhanuka
5 min readJul 15, 2022

--

From the beginning of the article, Let’s discuss some basics of this topic, What is TCP Standard for? TCP stands for Transmission Control Protocol which indicates that it does something to control the transmission of the data in a reliable way. The communication process between devices over the internet happens according to the current TCP/IP suite model. Remember the OSI model? Let’s briefly discuss this topic.

  • OSI Model…
OSI Layers

The Open Systems Interconnection (OSI) model defines seven layers where computer systems communicate over a network. The current Internet is not built on OSI but the simpler TCP/IP architecture. However, the OSI 7-layer model is still commonly used since it helps visualize and communicate how networks work and isolate and troubleshoot networking problems.

TCP Message Types…

  • SYN — Used to initiate and sustain a connection. It also helps you to synchronize sequence numbers between devices.
  • ACK — It is important to confirm to the other side that the SYN has been received.
  • SYN-ACK —SYN message from local device and ACK of the earlier packet.
  • FIN — Used to end the connection.

Let’s discuss this using a simple use-case diagram.

According to this image, the client sends the request to the server. You can see that the SYN flag(Message Type) is enabled. also, you can see the SEQ(Sequence) is set to 0. which means this is the very first message. according to this scenario, we assume SEQ is 0, but in the real world, this SEQ will be any random number. we used 0 here because it is easy to understand.

Now the server sees the message and the server responds with ACK(Acknowledgement). ACK is 1. While sending the ACK it also sends the SEQ. Now the client has to respond to that message. The client sends an ACK message to the server. ACK is 1 and SEQ is also set to 1. now at this point, the communication between the client and the server will be established.

Now during this client and server communication, we have a window update. either client side or server side when they communicate their window size, “how many bytes you can send before my buffer gets filled?” if the buffer gets filled they send the buffer filled flag to the other side. according to this Usecase, you can see(WU) ACK and SEQ are 1 and LEN(Length) is 0.

Now after this immediately, the server sends a “Hello Client” message to the client and disconnects. ACK and SEQ are now set to 1 because they are expecting these values. now LEN is 12 because “Hello Client” has 12 lengths. Now the client needs to send the message to the server. Now ACK is 13 and SEQ is 1. How that ACK is set to 13? Well, that previous message from the server ACK is 1 and LEN is 12, So 12+1 is 13 then ACK is set to 13 here. Now that the message transaction is complete. now that connection needs to terminate. Now server sends the message back to the client using the FIN flag. Now SEQ is 13 because that last ACK was 13. Here ACK is 1.

Now client sends the ACK message to the server. To respond to that FIN message client sends the ACK message to the server. Now ACK is 14 because the previous SEQ is 13 and ACK is 1. So, 13+1 is 14 and in the last message SEQ is set to 1. meantime the client also sends the FIN flag to the server to terminate the process. Then the server sends the last ACK message to the client and now the message communication and connection establishment are done.

Now I’ll show you how this can work practically, here we have a simple client-server application.

Client.js
Server.js

Now I’m going to start the server.js and client.js. I used Wireshark for this demo. I set “tcp.port==42181” to filter out my application results.

Here you can see all the status of the client and server sides.

SYN flag is enabled.

Here you can see SYN is set to 1. port 52105 is a server port. it's dynamic. Then the client sends it to the server and the server sends it to the client.

Now here server set the message length and sends the actual message to the client. you can see the “hello client” message in Wireshark.

Server Terminating the connection.

The client sends an ACK message and the FIN flag.

And finally, the server sends the ACK message. here the message communication and connection establishment are done.

This is how TCP works.

Thank you…

References :

https://www.youtube.com/watch?v=UpUd5zEUUgI

--

--