Skip to content

This repository contains a minimal peer-to-peer chat prototype that uses a central server for registration and bridging, then lets clients connect directly to each other for real-time text chat.

Notifications You must be signed in to change notification settings

alaaddingh/p2p_chat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

P2P Chat with Registration and Bridging

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.


Overview

  • Server (server.py):
    • Maintains a list of connected clients (client ID, IP, port).
    • Accepts REGISTER, BRIDGE, and INFO requests 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.

Goals

  • 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! :)

Instructions

1. Setup

  • Install Python 3 on your system.
  • Clone this repository or download the server.py and client.py files.

2. Run the Server

python server.py

The server listens on 127.0.0.1:5555 by default.

3. Run a Client

python client.py <clientID> <listenPort> 127.0.0.1:5555

Example:

python client.py alice 5556 127.0.0.1:5555

4. Commands

Command Description
/registerRegisters this client with the server.
/infoRequests a list of all known clients.
/bridgeRequests another client's IP/Port for direct connection.
/chat IP:PORTConnects directly to a peer’s chat socket.
/msg <text>Sends a text message to the connected peer.
/quitCloses the client.

5. Running Multiple Clients

  • Run another client in a new terminal:
python client.py bob 5557 127.0.0.1:5555

Example Usage Flow

On 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. ---

Quitting

  • Client: Type /quit at the prompt.
  • Server: Press CTRL + C in the terminal.

Learning Outcomes

  • 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.

About

This repository contains a minimal peer-to-peer chat prototype that uses a central server for registration and bridging, then lets clients connect directly to each other for real-time text chat.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages