Introduction:
In this project, we will create a basic chat application using Python. The application will allow two users to send messages to each other over a network. We will be using Python’s socket library to create a server-client model, where the server will receive messages from the client and relay them to other clients. This is a great beginner project for understanding networking and communication in Python.
Objective:
The main goal of this project is to implement a simple, text-based chat application that can be used for direct communication between two users. The program will have two parts:
- Server: It will accept connections from clients and relay messages between them.
- Client: It will allow users to send and receive messages through the server.
Python Code for the Chat Application
Server Code
import socket import threading # Function to handle client messages def handle_client(client_socket): while True: try: message = client_socket.recv(1024).decode('utf-8') if message: print(f"Received message: {message}") broadcast(message, client_socket) else: break except: break # Function to broadcast messages to all clients def broadcast(message, client_socket): for client in clients: if client != client_socket: try: client.send(message.encode('utf-8')) except: continue # Set up the server server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind(('0.0.0.0', 12345)) server.listen(5) print("Server started, waiting for clients to connect...") clients = [] # Accept client connections while True: client_socket, client_address = server.accept() print(f"Client connected from {client_address}") clients.append(client_socket) # Start a new thread to handle the client client_thread = threading.Thread(target=handle_client, args=(client_socket,)) client_thread.start()
Client Code
import socket import threading # Function to send messages to the server def send_message(client_socket): while True: message = input("Enter your message: ") client_socket.send(message.encode('utf-8')) # Function to receive messages from the server def receive_message(client_socket): while True: try: message = client_socket.recv(1024).decode('utf-8') if message: print(f"New message: {message}") except: break # Set up the client client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect(('127.0.0.1', 12345)) # Start threads for sending and receiving messages send_thread = threading.Thread(target=send_message, args=(client,)) receive_thread = threading.Thread(target=receive_message, args=(client,)) send_thread.start() receive_thread.start()
Explanation of Program Structure
Server:
– The server uses the socket
library to bind to a specific IP address and port (in this case, 0.0.0.0
for all available interfaces and port 12345
).
– It listens for incoming client connections and starts a new thread to handle each client’s communication.
– The handle_client
function listens for messages from the client, and the broadcast
function sends these messages to all other connected clients.
Client:
– The client establishes a connection with the server via the connect()
function.
– Two threads are started for sending and receiving messages.
– The user enters a message in the console, which is sent to the server. The client also listens for incoming messages and displays them in the console.
How to Run the Program
- First, save the server code in a file called server.py and the client code in a file called client.py.
- Run the server by executing the command
python server.py
in the terminal. This will start the server and make it wait for client connections. - Next, run the client by executing
python client.py
in another terminal or another machine on the same network. The client will connect to the server and allow you to send and receive messages. - Multiple clients can be connected at the same time, and each will be able to send and receive messages in real-time.