This repository demonstrates basic TCP socket programming in Python through a simple client-server model. It contains two scripts:
TCPServer.py
: A TCP server that listens for incoming client connections, processes messages, and sends responses.TCPClient.py
: A TCP client that connects to the server, sends user input, and displays the server's response.
- TCP Server: Listens on a configurable port, accepts incoming client connections, receives data, processes it (converts it to uppercase), and sends a response back to the client.
- TCP Client: Connects to the TCP server, sends a message (entered by the user), receives the server's response, and prints it.
This script implements a simple TCP server. Its key steps:
-
Create a TCP Socket:
Usessocket(AF_INET, SOCK_STREAM)
for IPv4 and TCP. -
Bind to a Port:
Binds the server to all interfaces (''
) and port12332
. -
Listen for Connections:
Listens for up to 5 simultaneous connection requests. -
Accept Connections:
Waits in an infinite loop to accept new connections. -
Process Client Data:
- Receives a sentence from the client.
- Converts it to uppercase.
- Sends the capitalized sentence back to the client.
- Closes the client connection.
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', 12332))
serverSocket.listen(5)
print("server is ready to receive")
while True:
connectionSocket, addr = serverSocket.accept()
sentence = connectionSocket.recv(1024).decode()
captalizedSentence = sentence.upper()
connectionSocket.send(captalizedSentence.encode())
connectionSocket.close()
This script acts as a client that connects to the above server. Its key steps:
-
Create a TCP Socket:
Usessocket(AF_INET, SOCK_STREAM)
. -
Connect to the Server:
Connects to the server using the specified IP address and port (192.168.0.103
,12332
).
Note: Change the IP address to match your server's address. -
Send Data:
Prompts the user for input, encodes it, and sends it to the server. -
Receive and Display the Response:
Receives the server's reply and prints it. -
Close the Connection:
Closes the socket.
from socket import *
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect(('192.168.0.103', 12332))
sentence = input('Input lowercase sentence: ')
clientSocket.send(sentence.encode())
ackSentence = clientSocket.recv(1024)
print("From server: ", ackSentence.decode())
clientSocket.close()
- Python 3.x
No external dependencies are required; only the Python standard library is used.
On the server machine (or terminal window), run:
python TCPServer.py
You should see:
server is ready to receive
On the client machine (or a different terminal window on the same machine), edit the TCPClient.py
file to set the correct server IP address:
clientSocket.connect(('SERVER_IP_ADDRESS', 12332))
Replace SERVER_IP_ADDRESS
with the actual IP address of the server.
Then run:
python TCPClient.py
You will be prompted:
Input lowercase sentence:
Type a sentence (e.g., hello world
) and press Enter.
The client will send this to the server, and print the response:
From server: HELLO WORLD
- Make sure the server and client are on the same network or the server's port is accessible from the client.
- The server will process one connection at a time and then wait for the next client.
- You can extend this example to handle multiple clients concurrently using Python threading or asynchronous sockets.
This project is for educational purposes and does not include a specific license. Feel free to use, modify, and share.