Skip to content

Siddesh-bype/oops_Socket_Programming_chat_Application

Repository files navigation

OOPS Socket Programming Chat Application

A comprehensive chat application consisting of a C++ TCP server, C++ console client, and Python Tkinter GUI client that all interoperate seamlessly.

Project Structure

/server.cpp         - C++ TCP server with multithreading support
/client.cpp         - C++ console client with full-duplex I/O
/client_gui.py      - Python Tkinter GUI client
/README.md          - This file
/chat.log           - Created at runtime for message logging

Features

Server (server.cpp)

  • Object-oriented design with Connection base class, Server derived class, and ClientHandler composition
  • TCP socket communication with IPv4 support
  • Multithreaded architecture handling up to 100 concurrent clients using std::thread
  • Thread-safe operations with std::mutex protection for shared state
  • Message broadcasting - forwards messages from one client to all connected clients
  • Private messaging support with /msg <username> <text> command
  • User management with unique username validation
  • Message logging - timestamped messages appended to chat.log using fstream
  • Graceful shutdown handling SIGINT/SIGTERM with proper socket cleanup
  • Robust error handling for all socket operations

Console Client (client.cpp)

  • Full-duplex I/O with separate threads for sending and receiving
  • Command support for /quit, /msg <username> <text>, and /who
  • Real-time message display with server-provided timestamps
  • Graceful disconnection handling
  • Signal handling for clean shutdown on Ctrl+C

Python GUI Client (client_gui.py)

  • Tkinter-based graphical interface with scrollable chat history
  • Background threading for non-blocking message reception
  • Thread-safe message queue for GUI updates
  • Real-time chat with automatic scrolling
  • Command support matching console client functionality
  • Connection status display and error handling
  • Entry field bound to Enter key for quick messaging

Protocol Specification

Message Format

  • Username transmission: First message from client is the chosen username (newline-terminated)
  • Regular messages: Newline-terminated strings
  • Server formatting: Messages prefixed with [HH:MM:SS] <username>: <text>
  • Private messages: Prefixed with [HH:MM:SS] [PM] <sender>: <text>
  • System messages: Server announcements for join/leave events

Supported Commands

  • /quit - Disconnect from server gracefully
  • /msg <username> <text> - Send private message to specific user
  • /who - List all connected users

Build & Run Instructions

Prerequisites

  • C++ compiler with C++17 support (g++ recommended)
  • Python 3 with Tkinter (usually included in standard installations)
  • POSIX-compliant system (Linux, macOS, or WSL on Windows)

Building C++ Components

# Build server
g++ -std=c++17 -pthread server.cpp -o server

# Build console client  
g++ -std=c++17 -pthread client.cpp -o client

Running the System

  1. Start the server:
./server 5555
  1. Connect console clients:
./client 127.0.0.1 5555
  1. Connect GUI client:
python3 client_gui.py 127.0.0.1 5555

Testing Checklist

Basic Functionality

  • Start server: ./server 5555 - server should start and listen on port 5555
  • Connect multiple console clients: ./client 127.0.0.1 5555 with different usernames
  • Message broadcasting: Send messages from one client, verify they appear on all others
  • Message logging: Check that messages are logged to chat.log with timestamps
  • Username uniqueness: Try connecting with duplicate username, should be rejected

Command Testing

  • Private messaging: From one client use /msg <username> <message>, verify target receives it
  • User listing: Use /who command, verify it returns comma-separated list of users
  • Graceful disconnect: Use /quit command, other clients should see disconnect message
  • Server persistence: Disconnect one client, verify server continues running for others

GUI Client Testing

  • GUI connection: python3 client_gui.py 127.0.0.1 5555 should show GUI window
  • Cross-platform messaging: Send messages between GUI and console clients
  • Real-time updates: Messages should appear immediately in GUI chat window
  • Command support: GUI client should support all commands (private messages, user list)
  • Connection handling: Close GUI window should disconnect gracefully

Error Handling

  • Invalid server: Try connecting to non-existent server, should show error
  • Server shutdown: Stop server while clients connected, clients should detect disconnect
  • Network interruption: Simulate network issues, clients should handle gracefully
  • Invalid commands: Send malformed commands, server should handle without crashing

Performance Testing

  • Multiple clients: Connect 10+ clients simultaneously
  • Message flooding: Send rapid messages, server should handle without blocking
  • Long-running: Leave server running for extended period, should remain stable

Network Configuration

Port Settings

  • Default port: 5555
  • Binding: Server binds to 0.0.0.0 (all interfaces)
  • Client connection: Clients connect to specified IP and port

Replit-Specific Notes

  • Ensure server port (5555) is properly exposed in Replit environment
  • If inbound connections are limited, run all components within same Replit shell sessions
  • Use 0.0.0.0 for server binding to accept connections from all interfaces

Troubleshooting

Common Issues

  1. "Address already in use": Wait a few seconds after stopping server, or use different port
  2. "Connection refused": Ensure server is running before starting clients
  3. "Permission denied": Check firewall settings and port permissions
  4. GUI not starting: Verify Python3 and Tkinter are properly installed

Debug Mode

Add -g -DDEBUG flags when compiling for additional debug output:

g++ -std=c++17 -pthread -g -DDEBUG server.cpp -o server
g++ -std=c++17 -pthread -g -DDEBUG client.cpp -o client

Architecture Notes

Thread Safety

  • Server uses std::mutex for protecting shared client list and logging operations
  • All socket operations are handled with proper error checking
  • Client disconnections are handled gracefully with thread cleanup

Memory Management

  • Uses std::shared_ptr for automatic client handler cleanup
  • RAII principles applied for socket and file resource management
  • No memory leaks in normal operation

Scalability

  • Server supports up to 100 concurrent connections (configurable)
  • Message broadcasting is O(n) where n is number of connected clients
  • Log file grows indefinitely (consider rotation for production use)

License

This project is provided as educational material. Feel free to modify and distribute.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published