TCP 3-Way Handshake Process

Udara nadeeshani
4 min readJun 28, 2022

--

What is TCP?

TCP stands for Transmission Control Protocol and it is one of the TCP/IP internet protocols.

From the application layer, the information is transferred to the transport layer where TCP protocol was implement. In transport layer there are 2 important protocols. Those are TCP, UDP(User Datagram Protocol).

  • TCP is a connection-oriented protocol: it means that the client has to establish a connection with a server before exchanging data with it.
  • TCP is reliable: it means that the sender is notified when its message is received by the receiver
  • The data is re-transmitted if the receiver didn’t receive the message, so no data is lost
  • Your data is delivered in order, which means that the receiver will receive packets in the same order in which the sender has sent them.
  • Error detection.

TCP provides reliable communication with something called Positive Acknowledgement with Re-transmission(PAR). The Protocol Data Unit(PDU) in transport layer is called a segment. When device send the request using PAR, it resend the data until it receives an acknowledgement. If the receiver received data was damaged, receiver discards the segment. So the sender has to resend the data unit for which positive acknowledgement is not received until positive acknowledgement is received.

TCP 3-Way Handshake

To established reliable TCP connection three segments are exchanged between client (sender) and server (receiver).

Step 1:

  • Client sends a segment with SYN (Synchronize Sequence Number) which informs the server that the client is likely to start communication and with what sequence number (seq ) it starts segments with. (seq = 0)

SYN flag is initially sent when establishing the classical 3-way handshake between two hosts. During the 3-way handshake we are able to count a total of 2 SYN flags transmitted, one by each host.

Step 2:

  • Server responds to the client request with ACK (Acknowledgement) signifies the response of the segment it received
  • SYN signifies with what sequence number server is likely to start the segments with. Ack = 1.

ACK acknowledgement flag is used to acknowledge the successful receipt of packets.

Step 3:

  • Client responds to the server request (SYN) with ACK acknowledges the response of the server and they both establish a reliable connection. In step 2 ack=1 is expected value, So in this step Client assign seq=1 as request in step 2.

Lets consider scenario when Client send the request Server will respond with “hello client” and end the connection.

Step 1, Step 2, Step 3 we discussed earlier.

Step 4:

  • Server responds to the client response(Ack) with ACK (Ack =1) and send the WU (Window Update) to the client .

Window Update : How many bytes can send before buffer is filled (window size). When communicating client and server communicate in window size.

Step 5:

  • Server send PSH (push) with “hello client” message to the client to push. Length of characters is 12. (len=12)

PSH flag in the TCP header informs the receiving host that the data should be pushed up to the receiving application immediately.

Step 6:

  • Client responds to the server request (PSH) with ACK (Ack =13) acknowledges the response of the server.

Ack =13 : (last ack=1) + (len =12)

Step 7:

  • Server responds to the client response(Ack) with ACK.
  • Now Server want to end the connection. So server send FIN (Finished) which informs the Client that the Server is likely to end the communication. (seq=13 because last Ack=13)

Step 8:

  • Client responds to the server request(FIN) with ACK acknowledges the request of the server. (Ack =14 because last seq=13 + last ack=1)

Step 9:

  • Client send FIN (Finished) which informs the server that the Client is likely to end the communication.

Step 10:

  • Finally , Server responds to the client request to end the communication with ACK . (seq = 14, ack =2)

Implementation

Client.java

var net = require('net');var port = 8888;
var host = "127.0.0.1";
var client = new net.Socket();client.connect(port,host , ()=>{
console.log(`connected to server: ${host} on ${port} `);
});
client.on('data', (data)=>{
console.log(`Received : ${data}`);
});
client.on('close', ()=>{
console.log("Client is connected from the server");
});

Server.Java

var net = require('net');var port = 8888;
var host = "127.0.0.1";
var server = net.createServer( (socket) => {
socket.on ('end', ()=> {
console.log("Server : Client disconnected");
});
});
server.on("connection" , (socket) =>{
console.log(`Connected from : ${socket.remoteAddress} : ${socket.remotePort}`);
socket.write("hello client");
socket.end();
});
server.on('error', (err)=>{
throw err;
});
server.listen(port, host);

Process

--

--