A simple, real-time chat system with client-server architecture built in C. The system allows users to register, authenticate, create chat rooms, join existing rooms, and exchange messages with other users in the same room.
- User registration and authentication
- Chat room creation and management
- Real-time messaging within rooms
- Multi-client support
- Command-line interface for both client and server
.
├── src/ # Source code
│ ├── client/ # Client application
│ │ ├── client.c # Main client implementation
│ │ ├── client.h # Client header file
│ │ ├── client_ui.c # Client user interface
│ │ ├── client_network.c # Client network handling
│ │ └── CMakeLists.txt # Client build configuration
│ ├── server/ # Server application
│ │ ├── server.c # Main server implementation
│ │ ├── server.h # Server header file
│ │ ├── server_room.c # Server room management
│ │ ├── server_client.c # Server client handling
│ │ ├── server_auth.c # Server authentication logic
│ │ └── CMakeLists.txt # Server build configuration
│ ├── common/ # Shared code between client and server
│ │ ├── include/ # Common header files
│ │ │ ├── database.h # Database interface
│ │ │ ├── message.h # Message handling
│ │ │ ├── protocol.h # Communication protocol
│ │ │ └── utils.h # Utility functions
│ │ ├── src/ # Common source files
│ │ │ ├── database.c # Database implementation
│ │ │ ├── message.c # Message creation and parsing
│ │ │ ├── protocol.c # Protocol implementation
│ │ │ └── utils.c # Utility functions
│ │ └── CMakeLists.txt # Common build configuration
│ └── CMakeLists.txt # Main source build configuration
└── CMakeLists.txt # Main project build configuration
- C compiler (GCC or Clang)
- CMake (version 3.10 or higher)
- SQLite3 development libraries
- POSIX-compliant operating system (Linux, macOS, or WSL for Windows)
- pthread library
- Clone the repository
- Create a build directory and navigate to it:
mkdir -p build cd build
- Configure the project with CMake:
cmake ..
- Build the project:
make
The executables will be created in the bin
directory.
./bin/chat_server [options]
Options:
-d, --db PATH
- Database path (default:../chat.db
)-p, --port PORT
- Port to listen on (default:8080
)-h, --help
- Show help message
Example:
./bin/chat_server -p 9000 -d /path/to/custom.db
./bin/chat_client [options]
Options:
-h, --host HOST
- Server hostname (default:127.0.0.1
)-p, --port PORT
- Server port (default:8080
)--help
- Show help message
Example:
./bin/chat_client -h chat.example.com -p 9000
- Start the server with desired options
- The server will automatically create the database if it doesn't exist
- Keep the server running to allow clients to connect
- Start the client and connect to the server
- Register a new account or login with existing credentials
- Create a new chat room or join an existing one
- Start chatting with other users in the same room
The client and server communicate using a custom binary protocol with different message types:
- Authentication requests/responses
- Registration requests/responses
- Room creation requests/responses
- Room joining/leaving requests/responses
- Chat messages
- Error messages
Each message has a header specifying the message type and length, followed by message-specific data.
If you find this project useful, consider buying me a coffee!