This repository contains a minimal peer-to-peer chat program that uses a central server for registration and bridging, then lets clients connect directly to each other for real-time text chat. The goal of this project was to learn about socket programming and basic protocol concepts, and was completed during my Introduction To Networking course.
- Server (
server.py):- Maintains a list of connected clients (client ID, IP, port).
- Accepts
REGISTER,BRIDGE, andINFOrequests from clients. - Provides bridging info (IP and port) of another client so a direct, peer-to-peer chat can be established.
- Client (
client.py):- Registers itself with the server using
/register. - Requests another client’s IP/port using
/bridge. - Opens a listening socket to accept inbound connections.
- Can initiate an outbound connection to another client, sending and receiving chat messages directly over TCP.
- Registers itself with the server using
- Educational Objective:
- Practice socket programming in Python.
- Explore how protocols introduce endpoints for direct communication.
- Understand multi-threaded designs necessary for real-time, two-way data exchange.
- To have fun! :)
- Install Python 3 on your system.
- Clone this repository or download the
server.pyandclient.pyfiles.
python server.pyThe server listens on 127.0.0.1:5555 by default.
python client.py <clientID> <listenPort> 127.0.0.1:5555Example:
python client.py alice 5556 127.0.0.1:5555| Command | Description |
|---|---|
/register | Registers this client with the server. |
/info | Requests a list of all known clients. |
/bridge | Requests another client's IP/Port for direct connection. |
/chat IP:PORT | Connects directly to a peer’s chat socket. |
/msg <text> | Sends a text message to the connected peer. |
/quit | Closes the client. |
- Run another client in a new terminal:
python client.py bob 5557 127.0.0.1:5555On Client A (Alice): Register with the server.
/register
On Client B (Bob): Register as well.
/register
On Client A: Request another client’s info.
/bridge
On Client A: Connect to Bob.
/chat 127.0.0.1:5557
On Client A: Send a message.
/msg Hello Bob!
On Client B: Receives and sees the message. ---
- Client: Type
/quitat the prompt. - Server: Press
CTRL + Cin the terminal.
- Socket Programming: Experience with both server and client TCP sockets in Python.
- Multi-Threading: Used separate threads for listening and sending, avoiding Windows console issues.
- Protocol Design: Implemented custom message headers (
REGISTER,BRIDGE,INFO) to establish communication. - P2P Communication: Demonstrated how a server can facilitate direct peer-to-peer chat.
This project serves as a simple demonstration of how clients can register with a central service and then connect peer-to-peer using minimal protocol logic.