Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn TCP: Connection Establishment and Termination | Transport Layer Protocols: TCP and UDP
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Network Protocols Deep Theory

bookTCP: Connection Establishment and Termination

When you use TCP to communicate over a network, you benefit from its reliable, connection-oriented approach. Establishing and terminating a TCP connection involves well-defined procedures to ensure that both endpoints are synchronized and that no data is lost or duplicated. The process begins with the three-way handshake, which is the method TCP uses to establish a connection. This handshake ensures that both client and server agree on initial sequence numbers and are ready to transmit data, preventing issues like old duplicate packets from previous connections.

The three-way handshake works as follows:

  1. The client sends a segment with the SYN (synchronize) flag set and an initial sequence number to the server;
  2. The server responds with a segment that has both the SYN and ACK (acknowledgment) flags set, acknowledging the client’s sequence number and providing its own sequence number;
  3. The client replies with a segment that has the ACK flag set, acknowledging the server’s sequence number, which completes the handshake.

This process ensures that both sides are aware of each other's initial sequence numbers and that the connection is fully established before data transfer begins.

Terminating a TCP connection is equally systematic, using a four-step process known as the four-way handshake. Either side can initiate termination by sending a segment with the FIN (finish) flag set. The sequence is:

  1. The endpoint initiating closure sends a FIN segment;
  2. The receiving endpoint replies with an ACK, confirming receipt of the FIN;
  3. The receiver then sends its own FIN when it is ready to close;
  4. The original sender replies with an ACK, completing the termination.

This sequence guarantees that both sides have finished sending data and that all data has been received before the connection is fully closed, avoiding data loss.

To visualize the state transitions during TCP connection establishment and teardown, consider the following pseudo code. This outlines how a TCP endpoint changes its state as it processes incoming and outgoing segments:

// Connection establishment (three-way handshake)
state = CLOSED
if (active_open):
    send(SYN)
    state = SYN_SENT
if (receive SYN):
    send(SYN+ACK)
    state = SYN_RECEIVED
if (receive SYN+ACK and state == SYN_SENT):
    send(ACK)
    state = ESTABLISHED
if (receive ACK and state == SYN_RECEIVED):
    state = ESTABLISHED

// Connection termination (four-way handshake)
if (close_requested):
    send(FIN)
    state = FIN_WAIT_1
if (receive FIN and state == ESTABLISHED):
    send(ACK)
    state = CLOSE_WAIT
if (receive ACK and state == FIN_WAIT_1):
    state = FIN_WAIT_2
if (receive FIN and state == FIN_WAIT_2):
    send(ACK)
    state = TIME_WAIT
if (timeout in TIME_WAIT):
    state = CLOSED
if (send FIN and state == CLOSE_WAIT):
    state = LAST_ACK
if (receive ACK and state == LAST_ACK):
    state = CLOSED

This pseudo code demonstrates the step-by-step state changes that occur during both connection setup and teardown, highlighting the importance of each acknowledgment and control flag.

A deeper look at the TCP state machine reveals how each state and flag contributes to the protocol’s reliability. When a connection is initiated, the client starts in the CLOSED state and transitions to SYN_SENT after sending a SYN. If the server receives the SYN, it moves from LISTEN to SYN_RECEIVED, sends a SYN-ACK, and waits for an ACK from the client. Once the ACK is received, both sides enter the ESTABLISHED state, ready for data transfer.

The SYN and SYN-ACK states prevent old or duplicate connection requests from interfering with new ones, ensuring that each connection is unique and synchronized. During termination, the FIN flag signals an endpoint’s intent to close, and the sequence of FIN and ACK exchanges ensures that both sides have finished sending and receiving data. The TIME_WAIT state is especially important: after an endpoint acknowledges the final FIN, it remains in TIME_WAIT to ensure that any delayed segments in the network are properly handled, preventing confusion with future connections that might reuse the same port numbers.

Each state in the TCP state machineβ€”such as SYN_SENT, SYN_RECEIVED, ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT, LAST_ACK, and TIME_WAITβ€”serves a specific purpose in managing the lifecycle of a connection and maintaining reliability, even in the face of network errors or retransmissions.

To capture the complexity of the TCP state machine, including handling of edge cases and errors, consider the following pseudo code for state transitions:

state = CLOSED
while (true):
    if (state == CLOSED and active_open):
        send(SYN)
        state = SYN_SENT
    elif (state == SYN_SENT and receive SYN+ACK):
        send(ACK)
        state = ESTABLISHED
    elif (state == SYN_SENT and timeout):
        retransmit(SYN)
    elif (state == SYN_RECEIVED and receive ACK):
        state = ESTABLISHED
    elif (state == ESTABLISHED and close_requested):
        send(FIN)
        state = FIN_WAIT_1
    elif (state == FIN_WAIT_1 and receive ACK):
        state = FIN_WAIT_2
    elif (state == FIN_WAIT_1 and receive FIN):
        send(ACK)
        state = CLOSING
    elif (state == FIN_WAIT_2 and receive FIN):
        send(ACK)
        state = TIME_WAIT
    elif (state == CLOSING and receive ACK):
        state = TIME_WAIT
    elif (state == TIME_WAIT and timeout):
        state = CLOSED
    elif (state == ESTABLISHED and receive FIN):
        send(ACK)
        state = CLOSE_WAIT
    elif (state == CLOSE_WAIT and close_requested):
        send(FIN)
        state = LAST_ACK
    elif (state == LAST_ACK and receive ACK):
        state = CLOSED
    // Error handling for duplicate or out-of-order packets
    elif (unexpected_packet):
        drop(packet)
    // Additional error handling for retransmissions
    elif (timeout and state in [SYN_SENT, FIN_WAIT_1]):
        retransmit(last_packet)

This pseudo code emphasizes how TCP handles various scenarios, including retransmissions due to timeouts and the dropping of unexpected or out-of-order packets, ensuring robust connection management.

1. Why does TCP use a three-way handshake instead of a two-way handshake for connection establishment?

2. What is the main purpose of the TIME_WAIT state in TCP?

3. Which of the following correctly describes the sequence of messages exchanged during TCP connection termination?

question mark

Why does TCP use a three-way handshake instead of a two-way handshake for connection establishment?

Select the correct answer

question mark

What is the main purpose of the TIME_WAIT state in TCP?

Select the correct answer

question mark

Which of the following correctly describes the sequence of messages exchanged during TCP connection termination?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookTCP: Connection Establishment and Termination

Swipe to show menu

When you use TCP to communicate over a network, you benefit from its reliable, connection-oriented approach. Establishing and terminating a TCP connection involves well-defined procedures to ensure that both endpoints are synchronized and that no data is lost or duplicated. The process begins with the three-way handshake, which is the method TCP uses to establish a connection. This handshake ensures that both client and server agree on initial sequence numbers and are ready to transmit data, preventing issues like old duplicate packets from previous connections.

The three-way handshake works as follows:

  1. The client sends a segment with the SYN (synchronize) flag set and an initial sequence number to the server;
  2. The server responds with a segment that has both the SYN and ACK (acknowledgment) flags set, acknowledging the client’s sequence number and providing its own sequence number;
  3. The client replies with a segment that has the ACK flag set, acknowledging the server’s sequence number, which completes the handshake.

This process ensures that both sides are aware of each other's initial sequence numbers and that the connection is fully established before data transfer begins.

Terminating a TCP connection is equally systematic, using a four-step process known as the four-way handshake. Either side can initiate termination by sending a segment with the FIN (finish) flag set. The sequence is:

  1. The endpoint initiating closure sends a FIN segment;
  2. The receiving endpoint replies with an ACK, confirming receipt of the FIN;
  3. The receiver then sends its own FIN when it is ready to close;
  4. The original sender replies with an ACK, completing the termination.

This sequence guarantees that both sides have finished sending data and that all data has been received before the connection is fully closed, avoiding data loss.

To visualize the state transitions during TCP connection establishment and teardown, consider the following pseudo code. This outlines how a TCP endpoint changes its state as it processes incoming and outgoing segments:

// Connection establishment (three-way handshake)
state = CLOSED
if (active_open):
    send(SYN)
    state = SYN_SENT
if (receive SYN):
    send(SYN+ACK)
    state = SYN_RECEIVED
if (receive SYN+ACK and state == SYN_SENT):
    send(ACK)
    state = ESTABLISHED
if (receive ACK and state == SYN_RECEIVED):
    state = ESTABLISHED

// Connection termination (four-way handshake)
if (close_requested):
    send(FIN)
    state = FIN_WAIT_1
if (receive FIN and state == ESTABLISHED):
    send(ACK)
    state = CLOSE_WAIT
if (receive ACK and state == FIN_WAIT_1):
    state = FIN_WAIT_2
if (receive FIN and state == FIN_WAIT_2):
    send(ACK)
    state = TIME_WAIT
if (timeout in TIME_WAIT):
    state = CLOSED
if (send FIN and state == CLOSE_WAIT):
    state = LAST_ACK
if (receive ACK and state == LAST_ACK):
    state = CLOSED

This pseudo code demonstrates the step-by-step state changes that occur during both connection setup and teardown, highlighting the importance of each acknowledgment and control flag.

A deeper look at the TCP state machine reveals how each state and flag contributes to the protocol’s reliability. When a connection is initiated, the client starts in the CLOSED state and transitions to SYN_SENT after sending a SYN. If the server receives the SYN, it moves from LISTEN to SYN_RECEIVED, sends a SYN-ACK, and waits for an ACK from the client. Once the ACK is received, both sides enter the ESTABLISHED state, ready for data transfer.

The SYN and SYN-ACK states prevent old or duplicate connection requests from interfering with new ones, ensuring that each connection is unique and synchronized. During termination, the FIN flag signals an endpoint’s intent to close, and the sequence of FIN and ACK exchanges ensures that both sides have finished sending and receiving data. The TIME_WAIT state is especially important: after an endpoint acknowledges the final FIN, it remains in TIME_WAIT to ensure that any delayed segments in the network are properly handled, preventing confusion with future connections that might reuse the same port numbers.

Each state in the TCP state machineβ€”such as SYN_SENT, SYN_RECEIVED, ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT, LAST_ACK, and TIME_WAITβ€”serves a specific purpose in managing the lifecycle of a connection and maintaining reliability, even in the face of network errors or retransmissions.

To capture the complexity of the TCP state machine, including handling of edge cases and errors, consider the following pseudo code for state transitions:

state = CLOSED
while (true):
    if (state == CLOSED and active_open):
        send(SYN)
        state = SYN_SENT
    elif (state == SYN_SENT and receive SYN+ACK):
        send(ACK)
        state = ESTABLISHED
    elif (state == SYN_SENT and timeout):
        retransmit(SYN)
    elif (state == SYN_RECEIVED and receive ACK):
        state = ESTABLISHED
    elif (state == ESTABLISHED and close_requested):
        send(FIN)
        state = FIN_WAIT_1
    elif (state == FIN_WAIT_1 and receive ACK):
        state = FIN_WAIT_2
    elif (state == FIN_WAIT_1 and receive FIN):
        send(ACK)
        state = CLOSING
    elif (state == FIN_WAIT_2 and receive FIN):
        send(ACK)
        state = TIME_WAIT
    elif (state == CLOSING and receive ACK):
        state = TIME_WAIT
    elif (state == TIME_WAIT and timeout):
        state = CLOSED
    elif (state == ESTABLISHED and receive FIN):
        send(ACK)
        state = CLOSE_WAIT
    elif (state == CLOSE_WAIT and close_requested):
        send(FIN)
        state = LAST_ACK
    elif (state == LAST_ACK and receive ACK):
        state = CLOSED
    // Error handling for duplicate or out-of-order packets
    elif (unexpected_packet):
        drop(packet)
    // Additional error handling for retransmissions
    elif (timeout and state in [SYN_SENT, FIN_WAIT_1]):
        retransmit(last_packet)

This pseudo code emphasizes how TCP handles various scenarios, including retransmissions due to timeouts and the dropping of unexpected or out-of-order packets, ensuring robust connection management.

1. Why does TCP use a three-way handshake instead of a two-way handshake for connection establishment?

2. What is the main purpose of the TIME_WAIT state in TCP?

3. Which of the following correctly describes the sequence of messages exchanged during TCP connection termination?

question mark

Why does TCP use a three-way handshake instead of a two-way handshake for connection establishment?

Select the correct answer

question mark

What is the main purpose of the TIME_WAIT state in TCP?

Select the correct answer

question mark

Which of the following correctly describes the sequence of messages exchanged during TCP connection termination?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 1
some-alt