Location>code7788 >text

TCP three handshakes and four waves.

Popularity:181 ℃/2024-08-14 17:53:01

TCP Three Handshakes and Four Waves Explained

In network communication, TCP (Transmission Control Protocol) is a very important protocol used to ensure that data can be transmitted reliably in unreliable network environments.TCP establishes a connection through a Three-way Handshake and terminates it through a Four-way Termination.


I. TCP three handshakes

TCP's three handshakes are used to ensure that the connection between the client and the server is reliable and that both parties are ready to transfer data. The process of the three handshakes is as follows:

  1. First handshake: client sends SYN

    • The client sends a SYN (Synchronization Sequence Number) message to the server indicating that the client wishes to establish a connection with the server. At this point, the client enters the SYN-SENT state.
    • The message contains an Initial Sequence Number (ISN) that is used for sequence control in subsequent data transfers.
  2. Second handshake: server sends SYN-ACK

    • After the server receives the SYN message, it sends a SYN-ACK (Synchronization Acknowledgement) message to the client indicating that it agrees to establish the connection. At this point, the server enters the SYN-RECEIVED state.
    • The SYN-ACK message contains the server's own ISN and the client's ISN+1, indicating that the server has received the client's SYN and is sending its own SYN to the client.
  3. Third handshake: client sends ACK

    • After receiving the SYN-ACK message, the client sends an ACK (acknowledgement) message to the server to confirm that the server's SYN has been received. At this point, the client enters the ESTABLISHED state and the connection is successfully established.
    • After the server receives the ACK message, it also enters the ESTABLISHED state, indicating that the connection is ready for data transfer.

With three handshakes, both parties confirm each other's sending and receiving capabilities and the connection is officially established.

 

This process is similar to the three processes involved in making a phone call:

1. Dialing. Dialing is equivalent to the first handshake.

2. Connect. The second handshake is when the other party is connected.

3. Response. The third handshake is when we hear a sound and respond.


II. Four waves of TCP

TCP's four waves are used to terminate the connection between the client and the server, ensuring that both parties are able to close the connection gracefully. The process of the four waves is as follows:

  1. First wave: client sends FIN

    • The client sends a FIN (termination) message indicating that the client has no more data to send and requests that the connection be closed. At this point, the client enters the FIN-WAIT-1 state.
    • The FIN message may contain the last segment of data, or it may simply indicate termination of transmission.
  2. Second wave: server sends ACK

    • After the server receives the FIN message, it sends an ACK message to the client to confirm that the client's FIN has been received. At this point, the server enters the CLOSE-WAIT state and the client enters the FIN-WAIT-2 state.
    • At this point, the server may still have unsent data and therefore will not close the connection immediately.
  3. Third wave: server sends FIN

    • After the server has sent all remaining data, it sends a FIN message to the client indicating that the server is also ready to close the connection. At this point, the server enters the LAST-ACK state.
    • After the client receives the FIN message, it sends the last ACK message to acknowledge receipt of the FIN from the server. at this point, the client enters the TIME-WAIT state.
  4. Fourth wave: client sends ACK

    • After the last ACK message sent by the client acknowledges the FIN message from the server, the client enters the TIME-WAIT state and waits for a certain period of time (usually two times the maximum message segment lifetime) to ensure that the server receives the ACK.
    • If no retransmitted FIN messages are received during the TIME-WAIT state, the client will enter the CLOSED state, indicating that the connection is completely closed.

With four waves of the hand, the TCP connection was able to close gracefully, ensuring that all data was transmitted and acknowledged in its entirety.

 

 

Example:

import socket
import time

# Server-side code
def server():
    # Creating a TCP socket
    server_socket = (socket.AF_INET, socket.SOCK_STREAM)
    # Binding IP address and port number
    server_socket.bind(('localhost', 12345))
    # Listen for connection requests
    server_socket.listen(1)
    print("Server is listening...")

    # Accepting client connections
    conn, addr = server_socket.accept()
    print(f"Connected by {addr}")

    # Analog server sends data
    (b'Hello, Client!')

    # Receiving client messages
    data = (1024)
    print(f"Received: {()}")

    # Close connection
    ()
    server_socket.close()
    print("Server connection closed.")

# Client Code
def client():
    # Creating a TCP socket
    client_socket = (socket.AF_INET, socket.SOCK_STREAM)
    # Connecting to the server
    client_socket.connect(('localhost', 12345))
    print("Client connected to server.")

    # Receiving server messages
    data = client_socket.recv(1024)
    print(f"Received: {()}")

    # Analog Client Sending Data
    client_socket.sendall(b'Thank you, Server!')

    # Close connection
    client_socket.close()
    print("Client connection closed.")

# Simulating TCP Three Handshakes and Four Waves
if __name__ == "__main__":
    # Start the server
    import threading
    server_thread = (target=server)
    server_thread.start()

    # Delay starting the client to make sure the server is up
    (1)

    # Starting the client
    client()

    # Waiting for the server thread to finish
    server_thread.join()

 

  • The server-side part:

    • utilization() Create a TCP socket and bind to thelocalhost uplink12345
    • pass (a bill or inspection etc)listen() method allows the server to start listening for client connections.
    • utilizationaccept() Accepts a connection request from the client and returns a new socketconn, which is used to communicate with the client.
    • The server sends a message to the client and receives the client's response before closing the connection.
  • Client Sections:

    • using the same() Creates a TCP socket.
    • pass (a bill or inspection etc)connect() method to connect to the server's IP and port.
    • The client receives the message from the server, sends a response message, and closes the connection.
  • Main program section:

    • Using Python'sthreading module to run the server and client simultaneously, simulating the three handshakes and four waves of the TCP process.

Output:

Server is listening...
Client connected to server.
Connected by ('127.0.0.1', random_port)
Received: Hello, Client!
Received: Thank you, Server!
Client connection closed.
Server connection closed.