Tag: TCP Tutorial

3 Way Handshake | TCP Connection

Transmission Control Protocol-

 

Before you go through this article, make sure that you have gone through the previous article on TCP in Networking.

 

We have discussed-

  • TCP establishes an end to end connection between the sender and receiver.
  • This connection is established before exchanging the data.
  • TCP connection is reliable and ensures in order delivery.

 

Also Read- TCP Header

 

In this article, we will discuss how a TCP connection is established.

 

3 Way Handshake-

 

Three Way Handshake is a process used for establishing a TCP connection.

 

Consider-

  • Client wants to establish a connection with the server.
  • Before Three Way Handshake, both client and server are in closed state.

 

TCP Handshake involves the following steps in establishing the connection-

 

Step-01: SYN-

 

For establishing a connection,

  • Client sends a request segment to the server.
  • Request segment consists only of TCP Header with an empty payload.
  • Then, it waits for a reply segment from the server.

 

 

Request segment contains the following information in TCP header-

  1. Initial sequence number
  2. SYN bit set to 1
  3. Maximum segment size
  4. Receiving window size

 

1. Initial Sequence Number-

 

  • Client sends the initial sequence number to the server.
  • It is contained in the sequence number field.
  • It is a randomly chosen 32 bit value.

 

2. SYN Bit Set To 1-

 

Client sets SYN bit to 1 which indicates the server-

  • This segment contains the initial sequence number used by the client.
  • It has been sent for synchronizing the sequence numbers.

 

Also Read- TCP Sequence Number

 

3. Maximum Segment Size (MSS)-

 

  • Client sends its MSS to the server.
  • It dictates the size of the largest data chunk that client can send and receive from the server.
  • It is contained in the Options field.

 

4. Receiving Window Size-

 

  • Client sends its receiving window size to the server.
  • It dictates the limit of unacknowledged data that can be sent to the client.
  • It is contained in the window size field.

 

Step-02: SYN + ACK-

 

After receiving the request segment,

  • Server responds to the client by sending the reply segment.
  • It informs the client of the parameters at the server side.

 

 

Reply segment contains the following information in TCP header-

  1. Initial sequence number
  2. SYN bit set to 1
  3. Maximum segment size
  4. Receiving window size
  5. Acknowledgment number
  6. ACK bit set to 1

 

1. Initial Sequence Number-

 

  • Server sends the initial sequence number to the client.
  • It is contained in the sequence number field.
  • It is a randomly chosen 32 bit value.

 

2. SYN Bit Set To 1-

 

Server sets SYN bit to 1 which indicates the client-

  • This segment contains the initial sequence number used by the server.
  • It has been sent for synchronizing the sequence numbers.

 

3. Maximum Segment Size (MSS)-

 

  • Server sends its MSS to the client.
  • It dictates the size of the largest data chunk that server can send and receive from the client.
  • It is contained in the Options field.

 

4. Receiving Window Size-

 

  • Server sends its receiving window size to the client.
  • It dictates the limit of unacknowledged data that can be sent to the server.
  • It is contained in the window size field.

 

5. Acknowledgement Number-

 

  • Server sends the initial sequence number incremented by 1 as an acknowledgement number.
  • It dictates the sequence number of the next data byte that server expects to receive from the client.

 

6. ACK Bit Set To 1-

 

  • Server sets ACK bit to 1.
  • It indicates the client that the acknowledgement number field in the current segment is valid.

 

Step-03: ACK-

 

After receiving the reply segment,

  • Client acknowledges the response of server.
  • It acknowledges the server by sending a pure acknowledgement.

 

 

With these, a Full Duplex connection is established.

 

Important Points-

 

Point-01:

 

In step-01 and step-02-

  • The connection parameters are established for the first side.
  • They are acknowledged by the second side.

 

In step-02 and step-03-

  • The connection parameters are established for the second side.
  • They are acknowledged by the first side.

 

Point-02:

 

Connection establishment phase consume 1 sequence number of both the sides.

 

  • Request segment consumes 1 sequence number of the requester.
  • Reply segment consumes 1 sequence number of the respondent.
  • Pure acknowledgements do not consume any sequence number.

 

Point-03:

 

Pure acknowledgement for the reply segment is not necessary.

 

This is because-

  • If client sends the data packet immediately, then it will be considered as an acknowledgement.
  • It means that in the first two steps only, the full duplex connection is established.

 

Point-04:

 

For all the segments except the request segment, ACK bit is always set to 1.

 

This is because-

  • For the request segment, acknowledgement number field will always be invalid.
  • For all other segments, acknowledgement number field will always be valid.

 

Point-05:

 

Certain parameters are negotiated during connection establishment.

 

The negotiation can be on setting the values of following parameters-

  1. Window size
  2. Maximum segment size
  3. Timer values

 

Point-06:

 

In any TCP segment,

  • If SYN bit = 1 and ACK bit = 0, then it must be the request segment.
  • If SYN bit = 1 and ACK bit = 1, then it must be the reply segment.
  • If SYN bit = 0 and ACK bit = 1, then it can be the pure ACK or segment meant for data transfer.
  • If SYN bit = 0 and ACK bit = 0, then this combination is not possible.

 

Point-07:

 

  • The combination SYN bit = 0 and ACK bit = 0 is not possible.
  • It is because SYN bit = 0 signifies it is not the request segment and reply segment.
  • For all other segments, ACK bit is always set to 1.

 

Point-08:

 

  • Consider sender sends the segments of size greater than MSS of receiver.
  • Then, they are first fragmented first at the receiver side.
  • It causes an extra overhead.

 

Point-09:

 

  • There is no dedicated field for sending MSS in TCP header.
  • This is because MSS has to informed only once.
  • So, if dedicated field would be present, then sending it each time would not be required.
  • For this reason, MSS is informed once using Options field.

 

Also Read- TCP Connection Termination

 

PRACTICE PROBLEMS BASED ON THREE WAY HANDSHAKE-

 

Problem-01:

 

SYN = 0 and ACK = 1 indicates-

  1. Open connection packet
  2. Open connection ACK
  3. Data packet
  4. ACK packet

 

Solution-

 

Options (C) and (D) are correct.

 

Problem-02:

 

An acknowledgement by TCP sender guarantees-

  1. Data has been delivered to the application
  2. Data has been received by TCP module
  3. Data has been received by application interface
  4. None of the above

 

Solution-

 

Option (A) is correct.

 

To gain better understanding about Three Way Handshake,

Watch this Video Lecture

 

Next Article- TCP Retransmission

 

Get more notes and other study material of Computer Networks.

Watch video lectures by visiting our YouTube channel LearnVidFun.

TCP Connection Termination | FIN Segment

Three Way Handshake-

 

Before you go through this article, make sure that you have gone through the previous article on Three Way Handshake.

 

We have discussed-

  • TCP uses Three Way Handshake to establish a connection between the sender and receiver.
  • Connection establishment using Three Way Handshake involves the steps as shown-

 

 

Also Read- TCP Retransmission

 

In this article, we will discuss how a TCP connection is terminated.

 

TCP Connection Termination-

 

A TCP connection is terminated using FIN segment where FIN bit is set to 1.

 

Consider-

  • There is a well established TCP connection between the client and server.
  • Client wants to terminate the connection.

 

The following steps are followed in terminating the connection-

 

Step-01:

 

For terminating the connection,

  • Client sends a FIN segment to the server with FIN bit set to 1.
  • Client enters the FIN_WAIT_1 state.
  • Client waits for an acknowledgement from the server.

 

 

Step-02:

 

After receiving the FIN segment,

  • Server frees up its buffers.
  • Server sends an acknowledgement to the client.
  • Server enters the CLOSE_WAIT state.

 

 

Step-03:

 

After receiving the acknowledgement, client enters the FIN_WAIT_2 state.

 

Now,

  • The connection from client to server is terminated i.e. one way connection is closed.
  • Client can not send any data to the server since server has released its buffers.
  • Pure acknowledgements can still be sent from the client to server.
  • The connection from server to client is still open i.e. one way connection is still open.
  • Server can send both data and acknowledgements to the client.

 

 

Step-04:

 

Now, suppose server wants to close the connection with the client.

 

For terminating the connection,

  • Server sends a FIN segment to the client with FIN bit set to 1.
  • Server waits for an acknowledgement from the client.

 

 

NOTE

If server wanted,

It could have sent the FIN segment along with the previous acknowledgment that it sent to the client.

 

Step-05:

 

After receiving the FIN segment,

  • Client frees up its buffers.
  • Client sends an acknowledgement to the server (not mandatory).
  • Client enters the TIME_WAIT state.

 

 

TIME_WAIT State-

 

  • The TIME_WAIT state allows the client to resend the final acknowledgement if it gets lost.
  • The time spent by the client in TIME_WAIT state depends on the implementation.
  • The typical values are 30 seconds, 1 minute and 2 minutes.
  • After the wait, the connection gets formally closed.

 

Next Article- Practice Problems On TCP

 

Get more notes and other study material of Computer Networks.

Watch video lectures by visiting our YouTube channel LearnVidFun.

TCP in Networking | TCP Protocol

Transport Layer Protocols-

 

There are mainly two transport layer protocols that are used on the Internet-

 

 

  1. Transmission Control Protocol (TCP)
  2. User Datagram Protocol (UDP)

 

In this article, we will discuss about Transmission Control Protocol (TCP).

Learn about User Datagram Protocol.

 

Transmission Control Protocol-

 

  • TCP is short for Transmission Control Protocol.
  • It is a transport layer protocol.
  • It has been designed to send data packets over the Internet.
  • It establishes a reliable end to end connection before sending any data.

 

Characteristics Of TCP-

 

Point-01:

 

TCP is a reliable protocol.

 

This is because-

  • It guarantees the delivery of data packets to its correct destination.
  • After receiving the data packet, receiver sends an acknowledgement to the sender.
  • It tells the sender whether data packet has reached its destination safely or not.
  • TCP employs retransmission to compensate for packet loss.

 

Read More- TCP Retransmission

 

Point-02:

 

TCP is a connection oriented protocol.

 

This is because-

  • TCP establishes an end to end connection between the source and destination.
  • The connection is established before exchanging the data.
  • The connection is maintained until the application programs at each end finishes exchanging the data.

 

Point-03:

 

TCP handles both congestion and flow control.

 

  • TCP handles congestion and flow control by controlling the window size.
  • TCP reacts to congestion by reducing the sender window size.

 

Read More- TCP Congestion Control

 

Point-04:

 

TCP ensures in-order delivery.

 

  • TCP ensures that the data packets get deliver to the destination in the same order they are sent by the sender.
  • Sequence Numbers are used to coordinate which data has been transmitted and received.

 

Point-05:

 

TCP connections are full duplex.

 

  • TCP connection allows to send data in both the directions at the same time.
  • So, TCP connections are Full Duplex.

 

Point-06:

 

TCP works in collaboration with Internet Protocol.

 

  • A TCP connection is uniquely identified by using-

Combination of port numbers and IP Addresses of sender and receiver.

  • IP Addresses indicate which systems are communicating.
  • Port numbers indicate which end to end sockets are communicating.
  • Port numbers are contained in the TCP header and IP Addresses are contained in the IP header.
  • TCP segments are encapsulated into an IP datagram.
  • So, TCP header immediately follows the IP header during transmission.

 

Point-07:

 

TCP can use both selective & cumulative acknowledgements.

 

  • TCP uses a combination of Selective Repeat and Go back N protocols.
  • In TCP, sender window size = receiver window size.
  • In TCP, out of order packets are accepted by the receiver.
  • When receiver receives an out of order packet, it accepts that packet but sends an acknowledgement for the expected packet.
  • Receiver may choose to send independent acknowledgements or cumulative acknowledgement.
  • To sum up, TCP is a combination of 75% SR protocol and 25% Go back N protocol.

 

Point-08:

 

TCP is a byte stream protocol.

 

  • Application layer sends data to the transport layer without any limitation.
  • TCP divides the data into chunks where each chunk is a collection of bytes.
  • Then, it creates a TCP segment by adding IP header to the data chunk.
  • TCP segment = TCP header + Data chunk.

 

Point-09:

 

TCP provides error checking & recovery mechanism.

 

TCP provides error checking and recovery using three simple techniques-

  1. Checksum
  2. Acknowledgement
  3. Retransmission

 

Also Read- Checksum

 

To gain better understanding about Transmission Control Protocol,

Watch this Video Lecture

 

Next Article- TCP Header

 

Get more notes and other study material of Computer Networks.

Watch video lectures by visiting our YouTube channel LearnVidFun.