Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python LAN Chat

A TCP-based chat application built in Python as part of my Computer Networks Lab course at the University of Kalyani. It features encrypted one-to-one chats using a custom RSA implementation and group messaging with Hamming Code-based error correction built from scratch. The project includes a tkinter-based GUI client and a pure Python server.

A screenshot showing two clients communicating

Features

  • Multi-Threaded Architecture : The server uses Python's threading module to handle multiple concurrent client connections smoothly.
  • One-on-One Encrypted Chat : Private messages are secured end-to-end using a from-scratch RSA encryption implementation. Public keys are automatically exchanged via the server as new clients register.
  • Group Chat with Error Correction : To simulate a noisy channel, the server intentionally introduces a random single-bit error into every group message. Clients use a custom Hamming Code implementation to automatically detect and correct the error before displaying the message.
  • Custom Application Protocol : A simple and robust |command:payload| text-based protocol coordinates all client-server communication.
  • Graphical User Interface : A minimal and responsive UI built with Python's native Tkinter library, running on a separate thread from the network listener to prevent freezing.

Setup & Usage

You will need Python 3.x installed. No external libraries are required.

  1. Clone the repository :

    git clone https://github.com/farhanr22/Python-LAN-Chat.git
    cd Python-LAN-Chat
  2. Run the server : Open a terminal and start the server. It will listen on 0.0.0.0:12345.

    python server.py
  3. Run clients : Open a new terminal for each client you want to connect (or connect from another system over LAN). The username must not contain | or :.

    # In another terminal
    # Usage: python client.py <ip>:<port> <username>
    python client.py 127.0.0.1:12345 Local_Client
    # On another system
    python client.py 192.168.2.10:12345 Remote_Client

How It Works

1-on-1 Chat (with RSA Encryption)

  1. Each client generates its own public/private RSA key pair (e, d, n) when it starts up.
  2. On connecting, the client sends its username and public key (e, n) to the server.
  3. The server registers the client and broadcasts their username and key pair to all other online clients, which then save this information.
  4. To send a private message, the sender's client looks up the recipient's public key, encrypts the message, and sends the encrypted message to the server.
  5. The server relays the encrypted payload to the recipient, who then decrypts it using their private key (d, n).

Group Chat (with Hamming Code-based error correction)

  1. A client sending a group message first converts the entire string to a single bit sequence and encodes it using Hamming code, which adds parity bits for redundancy.
  2. This encoded block is sent to the server, along with the length of the original data.
  3. To simulate transmission noise, the server intentionally flips a single random bit in the message.
  4. This corrupted message (along with the original data length) is broadcast to all other clients.
  5. Each receiving client's Hamming decoder inspects the parity bits, identifies the exact position of the flipped bit, corrects it, and decodes the message back to the original string. The corrupted version is logged to the terminal for comparison.

Application Protocol

The application uses a simple text-based protocol where all messages are framed by | delimiters.

Client-to-Server Commands :

  • IAM:<username>:<pk_e>:<pk_n> : Registers the client with a username and public key.
  • to:<recipient>:<encrypted_message> : Sends an RSA-encrypted private message.
  • groupsend:<data_length>:<message> : Sends a Hamming-encoded group message.

Server-to-Client Commands :

  • welcome:<message> : Confirms successful registration.
  • users:<user_data> : Provides a list of online users formatted as username1/pk_e1/pk_n1,username2/pk_e2/pk_n2,....
  • system:joined:<user_data> : Notifies all clients of a new user, with user_data formatted as username:pk_e:pk_n.
  • system:left:<username>: Notifies all clients that a user has disconnected.
  • from:<sender>:<encrypted_message> : Relays a private message from a sender to a recipient.
  • groupmsg:<sender>:<len>:<message> : Relays a group message (with a bit-flip) to all clients.

Modules

  • server.py : The multi-threaded TCP server logic for handling connections and relaying messages.
  • client.py : The client application, including the Tkinter GUI, networking thread, and command handlers.
  • rsa_module.py : A self-contained, from-scratch implementation of RSA key generation, encryption, and decryption.
  • hamming.py : A self-contained module for Hamming code encoding, decoding, and single-bit error correction.

License

This project is licensed under the MIT License.

About

TCP-based Chat application with encrypted 1-on-1 chats and error-correcting group messaging, built in Python.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages