askvity

How to Make a TCP Connection?

Published in Network Programming 5 mins read

To make a TCP connection, you need a TCP server listening for connections and a TCP client to initiate the connection. The client needs the server's IP address and port number.

Here's a breakdown of the process:

Understanding the Roles: Server and Client

  • TCP Server: A program that passively waits for incoming connection requests from clients. It listens on a specific port, ready to accept connections.
  • TCP Client: A program that actively initiates a connection to a server. It needs to know the server's IP address and the port number the server is listening on.

The TCP Handshake: Establishing the Connection

TCP uses a three-way handshake to establish a reliable connection:

  1. SYN (Synchronize): The client sends a SYN packet to the server, indicating its intention to establish a connection. This packet includes the client's initial sequence number.

  2. SYN-ACK (Synchronize-Acknowledge): The server receives the SYN packet and responds with a SYN-ACK packet. This packet acknowledges the client's SYN and also includes the server's initial sequence number.

  3. ACK (Acknowledge): The client receives the SYN-ACK packet and sends an ACK packet to the server, acknowledging the server's SYN. This completes the three-way handshake, and the TCP connection is established.

Steps to Establish a TCP Connection (Simplified):

  1. Server Setup:

    • Create a TCP socket on the server.
    • Bind the socket to a specific IP address and port.
    • Listen for incoming connections on that socket.
  2. Client Initiation:

    • Create a TCP socket on the client.
    • Specify the server's IP address and port number.
    • Initiate a connection to the server (which triggers the three-way handshake).
  3. Data Transfer: Once the connection is established, data can be sent back and forth between the client and the server.

Example (Conceptual):

Server (Python):

import socket

# Create a TCP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to an address and port
server_address = ('localhost', 12345)  # Example: listen on localhost, port 12345
server_socket.bind(server_address)

# Listen for incoming connections
server_socket.listen(1) # Allow one connection at a time

print('Server listening on', server_address)

# Accept a connection
connection, client_address = server_socket.accept()

print('Connection from', client_address)

#Receive data
data = connection.recv(1024)
print('Received', data.decode())

# Send data
connection.sendall(b'Hello Client')

#Close connection
connection.close()
server_socket.close()

Client (Python):

import socket

# Create a TCP socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Server address and port
server_address = ('localhost', 12345)

# Connect to the server
client_socket.connect(server_address)

# Send data
client_socket.sendall(b'Hello Server')

# Receive data
data = client_socket.recv(1024)
print('Received', data.decode())


#Close socket
client_socket.close()

Explanation of the Code:

  • socket.socket(socket.AF_INET, socket.SOCK_STREAM): Creates a TCP socket. AF_INET specifies IPv4, and SOCK_STREAM specifies TCP.
  • server_socket.bind(server_address): Associates the socket with a specific IP address and port.
  • server_socket.listen(1): Puts the server socket into listening mode, allowing it to accept incoming connections.
  • server_socket.accept(): Accepts an incoming connection, creating a new socket for communication with that client.
  • client_socket.connect(server_address): Initiates a connection to the server at the specified address and port.
  • connection.sendall(b'data') and client_socket.sendall(b'data'): Sends data to the other end of the connection. Data must be encoded as bytes.
  • connection.recv(1024) and client_socket.recv(1024): Receives data from the connection, up to 1024 bytes at a time.
  • connection.close() and client_socket.close(): Closes the socket connection.

Important Considerations:

  • Error Handling: Real-world applications require robust error handling to deal with connection failures, timeouts, and other potential issues.
  • Port Numbers: Use port numbers above 1023 for your applications, as ports below that are typically reserved for system services.
  • Firewalls: Firewalls can block TCP connections. Ensure that your firewall is configured to allow connections on the port you are using.
  • Security: For secure communication, consider using TLS/SSL to encrypt the TCP connection (e.g., using ssl.wrap_socket in Python).

In essence, creating a TCP connection involves setting up a server to listen for incoming connection requests and a client to initiate a connection to that server, all facilitated through the TCP three-way handshake.

Related Articles