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:
- Creates a TCP socket and binds it to port 12000
- Listens for incoming client connections
- Accepts a connection and receives a message (up to 1024 bytes)
- Converts the received message to uppercase
- Sends the capitalized message back to the client
- Closes the connection
- 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 timeTCPClient.py
The client performs the following operations:
- Specifies the server address and port
- Creates a TCP socket and connects to the server
- Prompts the user for input
- Sends the message to the server
- Receives the response (converted to uppercase)
- 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 IPTo find the server’s IP address:
- Linux/Mac: Run
ifconfigorip 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.45Usage
Starting the Server
python TCPServer.pyOutput: The server is ready to receive
Starting the Client
In another terminal (either on same machine or different machine):
python TCPClient.pyThen enter a lowercase sentence when prompted.
Example Session
Server Output:
The server is ready to receiveClient Output:
Input lowercase sentence: hello world
message from server: HELLO WORLDNotes
- 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)
