Python Socket Program for Client–Server Chat Communication
A Python-based TCP chat application using socket programming. Includes a server program that listens for connections and a client program that communicates with the server in real time. Useful for learning the fundamentals of networking, data transfer, and socket communication.
Explanation
A Client–Server Chat Application is a simple network-based communication system where two programs (client and server) exchange messages using TCP sockets. A server runs continuously, waits for the client to connect, receives text messages from the client, and sends replies back. The client connects to the server and communicates in real time.
This type of program helps understand how networking, sockets, ports,
data sending/receiving, and communication protocols work in Python.
The program uses the socket module, which provides low-level networking
interfaces to send and receive byte data over TCP connections.
Requirements
- Python version 3 or higher
- Two Python files: one for server and one for client
- Run server.py first, then client.py
- Both programs must be on same network (localhost for testing)
- Uses TCP Sockets through Python's
socketlibrary
Code Explanation (Server Side)
Imports Python's built-in socket module for network communication.
Creates a TCP socket using IPv4 addressing.
Binds server to the specified IP and port number.
Puts server into listening mode, waiting for incoming clients.
Accepts a client connection and returns connection object + client address.
Receives message from client and decodes bytes into string.
conn.send(reply.encode())
Server types a reply and sends it back to the client.
server.close()
Closes the connection and the server.
Code Explanation (Client Side)
Creates the client TCP socket.
Client connects to the server using same IP & port.
Client enters a message to send to server.
Sends encoded message to the server.
Receives the server’s reply and decodes it.
Closes the client connection.
Key Points
- Uses TCP (Transmission Control Protocol) for reliable communication.
- Client and server must use same host & port.
- Both programs communicate continuously inside loops.
- Data is sent in bytes and must be encoded/decoded.
- Socket must always be closed to free network resources.
Full Python Program (Server Side)
import socket
HOST = "127.0.0.1"
PORT = 5000
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen(1)
print(f"Server started at {HOST}:{PORT}")
print("Waiting for client connection...")
conn, addr = server.accept()
print(f"Client connected from: {addr}")
while True:
data = conn.recv(1024).decode()
if not data:
break
print("Client:", data)
reply = input("Server reply: ")
conn.send(reply.encode())
conn.close()
server.close()
print("Server closed.")
Full Python Program (Client Side)
import socket
HOST = "127.0.0.1"
PORT = 5000
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((HOST, PORT))
print(f"Connected to server {HOST}:{PORT}")
while True:
msg = input("Client message: ")
if msg.lower() == "exit":
break
client.send(msg.encode())
reply = client.recv(1024).decode()
print("Server:", reply)
client.close()
print("Client closed.")
Output :
-- SERVER SIDE --
Server started at 127.0.0.1:5000
Waiting for client connection...
Client connected from: ('127.0.0.1', 54022)
Client: Hello Server
Server reply: Hi Client
Client: How are you?
Server reply: I am fine!
-- CLIENT SIDE --
Connected to server 127.0.0.1:5000
Client message: Hello Server
Server: Hi Client
Client message: How are you?
Server: I am fine!
Client message: exit
Client closed.
