Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

Updated
5 min read
TCP Working: 3-Way Handshake & Reliable Communication

Before moving with TCP protocol, have you ever think about why all these rules are made? can’t we just simply send our data without any rules?

The Answer to this questions is No, we can’t just send the data on the network without rules , if we do then

  • data will become just random bits , means suppose you have send something to you friend without any protocols then if the message is sent succefully , you friend will not able to understand from where the message starts, where it ends , Like receiving a long string of letters with no spaces, no punctuation, no language.

  • without protocols sender doesn’t know wether it sends successfully or not, reciever can’t ask for missing data because again if reciever tries to ask for a missing data again without protocols some random data bit will gets generated and again the same issue will arise.

That’s why we need some rules (protocols) to send the data properly and here comes the concept of TCP.

What is TCP and why it is needed?

TCP(Transmission control protocol) is a protocol that allows devices to communicate reliably over a network, it ensures that data reaches the destination correctly and in right order, so that sender and reciever can make a reliable connection and able to send data to each other without worring about reliabliity. it also ensures that data reaches the destination correctly and in the right order , even if parts of network are slow or unreliable.

Problems it solves is

  • Data Loss → it ensures that these is no data loss while traveling accross networks.

  • Order of data → it ensures that data that send by the sender is recieved in the same order by reciever.

  • Duplicate data → it ensures that no duplicate data sends in the network.

  • Data corruption → Bits can be flipped during transmission, but TCP ensures that no bit is flipped using checkSum

What is the TCP 3-Way Handshake

TCP establishes a connection between a client and the server before sending any data, this is done using a process calles three-way handshake

  1. SYNC (synchronize) → client sends a SYN segment to the server, and ask “hey, I want to make a connection“

  2. SYNC-ACK (Synchronize-Acknowledge) → The server responds back to the client with a SYN-ACK segment and say “ok let’s make a connection

  3. ACK (Acknowledge) → The client replies with a ACK, saying “done. connection is established.“

This is how 3-way Handshake process takes place.

How data transfer works in TCP

  1. 3-Way Handshake → Before sending any data TCP establishedd a connection between client and server for secure and reliable data transfer

  2. Segmenting → When an application sends data (like an email) , TCP breaks that data into small chunks called segments. this makes it easier to send large amount of data over the network reliably.

    • Each segment has header containing information like sequence number, ports and flags

  3. Routing via IP → once TCP creates segments of data they are handed to IP and IP is responsible for delievering the segments from the client to the server.

  4. Acknowladgments (ACKs) → the reciever(client) sends an ACKs for every segment and this tells the sender that data has arrived safely , Suppose if ACKs is not revieved , TCP assumes that the segment was lost and triggers retransmission.

  5. Retransmission → if sender doesnt recieve an acknowledgment within a certain time then it resends the missing segment, This ensures no data is lost making TCP reliable.

  6. Flow & Error Control →

    • Flow Control : TCP prevents the sender from sending too much data too quickly for the server to handle.

    • Error Control : TCP checks for corrupt segment using checksums and request transmission if needed.

Together, these mechanisms ensure data is delivered reliably and efficiently, without overloading the network or the receiver.

How TCP connection is closed

For closing a TCP connection , we require a process call 4-way Handshake , to ensure that both sides finish transmitting data safely

  1. FIN (Finish) → the client who want to close the connection send a FIN segment to the server.

  2. ACK (Acknowledge) → the server acknowledges the FIN with an ACK.

  3. FIN (Finish) from Server → the server then sends its own FIN segment when it is ready to close the connection.

  4. ACK (Acknowledge) → the client responds with the ACK, finishing the termination.

This ensures that all the remaning data is securly transmittes before the connection is fully terminated.

Applications of TCP

  1. File transfer : Transferring a file over a network is such a big task because it contains a large amount of data, TCP breakes the large files into small pieces and sends them one by one and then put them back into the correct order at the recievers end and resends any piece that gets lost .

  2. Email : Sending email or revieving them requires that all the data must be complete and to the point that why TCP is used here.

  3. Web Browsing : Websites sends or recieves data in chunks which is called packets , TCP ensures that all the packets arrives completely and in order that’s how pages of the webpage loads correctly without missing images or broken content.