Basic Chatroom App
Abstract
Build a Basic Chatroom App that allows multiple users to communicate in real-time. The app includes both server and client implementations using sockets and threading. This project demonstrates network programming, concurrency, and user interaction in Python.
Prerequisites
- Python 3.6 or above
- Text Editor or IDE
- Basic understanding of Python syntax
- Familiarity with socket programming
- Knowledge of threading for concurrency
Getting Started
Creating a new project
- Create a new project folder and name it
basic_chatroom_app
basic_chatroom_app
. - Create a new file inside the folder and name it
basic_chatroom_app.py
basic_chatroom_app.py
. - Open the project folder in your favorite text editor or IDE.
- Copy the code below and paste it into the
basic_chatroom_app.py
basic_chatroom_app.py
file.
Write the code
⚙️ basic_chatroom_app.py
basic_chatroom_app.py
"""
Basic Chatroom App
A Python application that allows multiple users to communicate in a chatroom.
Features include:
- Server-side implementation to manage multiple clients.
- Client-side implementation for sending and receiving messages.
"""
import socket
import threading
# Server-side implementation
class ChatServer:
def __init__(self, host="localhost", port=12345):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind((host, port))
self.server.listen(5)
print(f"Server started on {host}:{port}")
self.clients = []
def broadcast(self, message, client_socket):
"""Send a message to all connected clients except the sender."""
for client in self.clients:
if client != client_socket:
try:
client.send(message)
except:
self.clients.remove(client)
def handle_client(self, client_socket):
"""Handle communication with a connected client."""
while True:
try:
message = client_socket.recv(1024)
if message:
print(f"Received: {message.decode('utf-8')}")
self.broadcast(message, client_socket)
except:
self.clients.remove(client_socket)
client_socket.close()
break
def run(self):
"""Start the server and accept incoming connections."""
while True:
client_socket, client_address = self.server.accept()
print(f"New connection: {client_address}")
self.clients.append(client_socket)
threading.Thread(target=self.handle_client, args=(client_socket,)).start()
# Client-side implementation
class ChatClient:
def __init__(self, host="localhost", port=12345):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client.connect((host, port))
threading.Thread(target=self.receive_messages).start()
def send_message(self, message):
"""Send a message to the server."""
self.client.send(message.encode("utf-8"))
def receive_messages(self):
"""Receive messages from the server."""
while True:
try:
message = self.client.recv(1024).decode("utf-8")
print(message)
except:
print("Disconnected from server.")
self.client.close()
break
if __name__ == "__main__":
choice = input("Do you want to start the server or client? (server/client): ").strip().lower()
if choice == "server":
server = ChatServer()
server.run()
elif choice == "client":
client = ChatClient()
print("Type your messages below:")
while True:
msg = input()
client.send_message(msg)
else:
print("Invalid choice. Exiting.")
basic_chatroom_app.py
"""
Basic Chatroom App
A Python application that allows multiple users to communicate in a chatroom.
Features include:
- Server-side implementation to manage multiple clients.
- Client-side implementation for sending and receiving messages.
"""
import socket
import threading
# Server-side implementation
class ChatServer:
def __init__(self, host="localhost", port=12345):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind((host, port))
self.server.listen(5)
print(f"Server started on {host}:{port}")
self.clients = []
def broadcast(self, message, client_socket):
"""Send a message to all connected clients except the sender."""
for client in self.clients:
if client != client_socket:
try:
client.send(message)
except:
self.clients.remove(client)
def handle_client(self, client_socket):
"""Handle communication with a connected client."""
while True:
try:
message = client_socket.recv(1024)
if message:
print(f"Received: {message.decode('utf-8')}")
self.broadcast(message, client_socket)
except:
self.clients.remove(client_socket)
client_socket.close()
break
def run(self):
"""Start the server and accept incoming connections."""
while True:
client_socket, client_address = self.server.accept()
print(f"New connection: {client_address}")
self.clients.append(client_socket)
threading.Thread(target=self.handle_client, args=(client_socket,)).start()
# Client-side implementation
class ChatClient:
def __init__(self, host="localhost", port=12345):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client.connect((host, port))
threading.Thread(target=self.receive_messages).start()
def send_message(self, message):
"""Send a message to the server."""
self.client.send(message.encode("utf-8"))
def receive_messages(self):
"""Receive messages from the server."""
while True:
try:
message = self.client.recv(1024).decode("utf-8")
print(message)
except:
print("Disconnected from server.")
self.client.close()
break
if __name__ == "__main__":
choice = input("Do you want to start the server or client? (server/client): ").strip().lower()
if choice == "server":
server = ChatServer()
server.run()
elif choice == "client":
client = ChatClient()
print("Type your messages below:")
while True:
msg = input()
client.send_message(msg)
else:
print("Invalid choice. Exiting.")
Key Features
- Server-side implementation to manage multiple clients
- Client-side implementation for sending and receiving messages
- Real-time communication using sockets
- Threading for handling multiple clients concurrently
Explanation
Server Implementation
The server listens for incoming connections and starts a new thread for each client:
basic_chatroom_app.py
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind((host, port))
self.server.listen(5)
threading.Thread(target=self.handle_client, args=(client_socket,)).start()
basic_chatroom_app.py
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind((host, port))
self.server.listen(5)
threading.Thread(target=self.handle_client, args=(client_socket,)).start()
Client Implementation
The client connects to the server and starts a thread to receive messages:
basic_chatroom_app.py
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client.connect((host, port))
threading.Thread(target=self.receive_messages).start()
basic_chatroom_app.py
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client.connect((host, port))
threading.Thread(target=self.receive_messages).start()
Message Broadcasting
The server broadcasts messages to all connected clients except the sender:
basic_chatroom_app.py
for client in self.clients:
if client != client_socket:
client.send(message)
basic_chatroom_app.py
for client in self.clients:
if client != client_socket:
client.send(message)
Running the Application
- Save the file.
- Run the server:
python basic_chatroom_app.py
python basic_chatroom_app.py
- Run the client in a separate terminal:
python basic_chatroom_app.py
python basic_chatroom_app.py
Conclusion
This Basic Chatroom App is a great introduction to network programming and concurrency in Python. You can extend it by adding user authentication, message history, or a GUI interface.
Was this page helpful?
Let us know how we did