# Network Chat Client

**Multi-threaded chat client with socket programming**

![Project Icon](/network-wired.png)

## Project Details

- **Author**: Molla Samser (Founder, RSK World)
- **Designer & Tester**: Rima Khatun
- **Website**: https://rskworld.in
- **Email**: info@rskworld.com, support@rskworld.com
- **Phone**: +91 93305 39277
- **Year**: 2026
- **Category**: C++ Projects
- **Difficulty**: Advanced

## Description

Create a multi-threaded chat client using C++ with socket programming. Features real-time messaging, user authentication, and encrypted communication. Perfect for learning C++ networking, multi-threading, and socket programming concepts.

## Technologies Used

- **C++** - Core programming language
- **Socket Programming** - Network communication
- **Multi-threading** - Concurrent message handling
- **Network Programming** - TCP/IP protocols
- **Winsock** - Windows socket API
- **Encryption** - OpenSSL for secure communication

## Features

- ✅ **Real-time messaging** - Instant message delivery
- ✅ **User authentication** - Secure login system
- ✅ **Encrypted communication** - Message encryption
- ✅ **Socket programming** - TCP/IP networking
- ✅ **Multi-threading** - Concurrent operations
- ✅ **Network programming** - Client-server architecture

## Project Structure

```
network-chat-client/
├── include/
│   ├── chat_client.h      # Client header file
│   └── chat_server.h      # Server header file
├── src/
│   ├── chat_client.cpp    # Client implementation
│   └── chat_server.cpp    # Server implementation
├── bin/
│   ├── chat_client.exe    # Compiled client
│   └── chat_server.exe    # Compiled server
├── docs/
│   └── documentation.md   # Additional docs
├── CMakeLists.txt         # Build configuration
├── README.md              # This file
└── LICENSE                # License information
```

## Installation & Setup

### Prerequisites

- Windows 10/11 (64-bit)
- Visual Studio 2019 or later
- OpenSSL library
- CMake (optional)

### Building from Source

#### Using Visual Studio

1. Clone or download the project
2. Install OpenSSL for Windows
3. Open the project in Visual Studio
4. Configure include and library paths for OpenSSL
5. Build the solution

#### Using CMake

```bash
# Create build directory
mkdir build
cd build

# Configure project
cmake ..

# Build
cmake --build .

# Run executables
./bin/chat_client.exe
./bin/chat_server.exe
```

### Dependencies

- **Winsock2** - Windows socket library
- **OpenSSL** - Encryption library (libcrypto, libssl)
- **C++ Standard Library** - Thread, mutex, atomic operations

## Usage

### Starting the Server

1. Run the server executable:
   ```bash
   chat_server.exe
   ```
2. Enter the port number (default: 54000)
3. Server will start listening for connections

### Connecting with Client

1. Run the client executable:
   ```bash
   chat_client.exe
   ```
2. Enter server IP (default: 127.0.0.1)
3. Enter port number (default: 54000)
4. Enter username and password for authentication
5. Start chatting!

### Client Commands

- `/help` - Show available commands
- `/quit` - Exit the chat client
- `/clear` - Clear the screen
- `/history` - Show message history
- `/private username message` - Send private message
- Any other text - Send broadcast message

### Server Commands

- `/help` - Show server commands
- `/stats` - Show server statistics
- `/history` - Show message history
- `/kick username` - Kick a user
- `/broadcast message` - Broadcast server message
- `/quit` - Stop the server

## Technical Implementation

### Socket Programming

The application uses Winsock2 for TCP/IP socket communication:

- **TCP Protocol**: Reliable, connection-oriented communication
- **Non-blocking sockets**: Efficient message handling
- **Thread-safe operations**: Mutex protection for shared resources

### Multi-threading Architecture

- **Client Threads**: 
  - Main thread for user input
  - Receive thread for incoming messages
  - Send thread for outgoing messages
- **Server Threads**:
  - Main thread for server commands
  - Accept thread for new connections
  - Client thread for each connected client

### Encryption

Messages are encrypted using OpenSSL:

- **Session Keys**: Unique keys per client session
- **XOR Encryption**: Simple demonstration (replace with AES in production)
- **Key Generation**: Cryptographically secure random keys

### Authentication System

- **Username Validation**: Unique usernames, length restrictions
- **Session Management**: Track client states and activity
- **Timeout Handling**: Automatic cleanup of inactive connections

## API Reference

### ChatClient Class

```cpp
class ChatClient {
public:
    bool initialize();
    bool start(const std::string& serverIP, int port);
    void stop();
    void run();
    void sendMessage(const std::string& content, const std::string& receiver = "");
    void sendBroadcast(const std::string& content);
    void sendPrivateMessage(const std::string& receiver, const std::string& content);
};
```

### ChatServer Class

```cpp
class ChatServer {
public:
    bool initialize(int port = DEFAULT_PORT);
    bool start();
    void stop();
    void run();
    int getConnectedClientsCount() const;
    std::vector<std::string> getOnlineUsers();
    void kickClient(const std::string& username);
    void broadcastServerMessage(const std::string& message);
};
```

## Configuration

### Default Settings

- **Server Port**: 54000
- **Max Clients**: 100
- **Buffer Size**: 4096 bytes
- **Timeout**: 5 minutes
- **Message History**: 1000 messages (server), 100 messages (client)

### Customization

You can modify these constants in the header files:

```cpp
#define MAX_CLIENTS 100
#define MAX_BUFFER_SIZE 4096
#define DEFAULT_PORT 54000
```

## Security Considerations

⚠️ **Important**: This is an educational project. For production use:

1. Replace XOR encryption with proper AES encryption
2. Implement proper user authentication with database
3. Add certificate-based SSL/TLS support
4. Implement rate limiting and DDoS protection
5. Add input validation and sanitization
6. Use proper error handling and logging

## Troubleshooting

### Common Issues

1. **Connection Failed**
   - Check if server is running
   - Verify IP address and port
   - Check firewall settings

2. **Authentication Failed**
   - Username might be already taken
   - Check username length (3-20 characters)

3. **Compilation Errors**
   - Ensure OpenSSL libraries are properly linked
   - Check Visual Studio configuration
   - Verify include paths

### Debug Mode

Enable debug output by uncommenting:
```cpp
#define DEBUG_MODE
```

## Contributing

Contributions are welcome! Please:

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Test thoroughly
5. Submit a pull request

## License

This project is developed by RSK World for educational purposes.

**Contact Information:**
- **Website**: https://rskworld.in
- **Email**: info@rskworld.com, support@rskworld.com
- **Phone**: +91 93305 39277
- **Address**: Nutanhat, Mongolkote, Purba Burdwan, West Bengal, India, 713147

## Educational Resources

This project is perfect for learning:

- **C++ Network Programming**: Socket APIs and TCP/IP
- **Multi-threading**: Thread management and synchronization
- **Encryption**: OpenSSL integration and cryptographic concepts
- **Client-Server Architecture**: Distributed systems design
- **Real-time Communication**: Message passing and protocols

## Acknowledgments

- **Molla Samser** - Project development and architecture
- **Rima Khatun** - Design, testing, and quality assurance
- **RSK World** - Educational platform and resources

---

© 2026 RSK World. All rights reserved.

*Content used for educational purposes only. View Disclaimer: https://rskworld.in/disclaimer.php*
