TCP Socket Simulation in python

GitHub code click here!

A simple TCP client-server communication example demonstrating bidirectional socket communication in Python.

Project Description

This project consists of two Python scripts that implement a basic TCP socket communication system:

  • TCPServer.py: A server that listens for incoming connections and converts received messages to uppercase
  • TCPClient.py: A client that connects to the server, sends a message, and receives the modified response

How It Works

TCPServer.py

The server performs the following operations:

  1. Creates a TCP socket and binds it to port 12000
  2. Listens for incoming client connections
  3. Accepts a connection and receives a message (up to 1024 bytes)
  4. Converts the received message to uppercase
  5. Sends the capitalized message back to the client
  6. Closes the connection
  7. Repeats the process for the next client
serverPort = 12000
serverSocket.bind(('', serverPort))  # Binds to all available network interfaces
serverSocket.listen(1)               # Accepts one connection at a time

TCPClient.py

The client performs the following operations:

  1. Specifies the server address and port
  2. Creates a TCP socket and connects to the server
  3. Prompts the user for input
  4. Sends the message to the server
  5. Receives the response (converted to uppercase)
  6. Displays the response and closes the connection

Important: Server IP Configuration

The serverIP variable in TCPClient.py must be set correctly based on where the scripts are running:

Scenario 1: Both Scripts on the Same Machine

Set the server IP to localhost or 127.0.0.1:

serverIP = '127.0.0.1'  # or 'localhost'

Scenario 2: Client and Server on Different Machines (Same Network)

Set the server IP to the actual IP address of the machine running the server:

serverIP = '192.168.1.10'  # Example: Replace with actual server machine IP

To find the server’s IP address:

  • Linux/Mac: Run ifconfig or ip addr show
  • Windows: Run ipconfig

Look for the non-localhost IP address (usually starting with 192.168.x.x, 10.x.x.x, or 172.16.x.x)

Scenario 3: Server Behind a Firewall (Different Networks)

If the server is behind a firewall or on a different network, you may need:

  • Port forwarding configuration
  • The server’s public/external IP address
  • Firewall rules that allow incoming connections on port 12000
serverIP = '<server-public-ip>'  # Example: 203.0.113.45

Usage

Starting the Server

python TCPServer.py

Output: The server is ready to receive

Starting the Client

In another terminal (either on same machine or different machine):

python TCPClient.py

Then enter a lowercase sentence when prompted.

Example Session

Server Output:

The server is ready to receive

Client Output:

Input lowercase sentence: hello world
message from server:  HELLO WORLD

Notes

  • The server only accepts one connection at a time and serves it synchronously
  • The server binds to all available network interfaces ('') on port 12000
  • Each message is limited to 1024 bytes
  • The client automatically closes the connection after receiving the response
  • For concurrent client handling, consider using threading or async approaches

Default Configuration

  • Server Port: 12000
  • Max Message Size: 1024 bytes
  • Connection Queue: 1 (only one connection at a time)

Leave a Reply

Your email address will not be published. Required fields are marked *.