Bachelor's Degree Project — Computer Networks Course (Reti Informatiche / Reti di Calcolatori)
This repository contains the implementation of a Multithreaded Client-Server Trivia Quiz Game written in C using POSIX Sockets and POSIX Threads (pthreads).
The project demonstrates fundamental networking and operating system concepts, including TCP/IP socket communication, concurrent client handling via thread pooling/spawning, dynamic memory allocation with linked lists, and thread synchronization using mutexes.
- TCP Client-Server Architecture: Reliable connection-oriented communication over TCP (default port
3456). - Concurrent Multi-Client Handling: Spawns a dedicated POSIX thread (
pthread_create) for each incoming client connection, allowing multiple players to connect and play simultaneously without blocking the main server loop. - Dynamic File-Based Trivia Loading: Automatically loads quiz categories, questions, and answers from text files into dynamically allocated singly linked lists at server startup.
- Thread-Safe State Management: Utilizes POSIX mutexes (
pthread_mutex_t) to prevent race conditions when accessing shared resources, such as global player lists and score leaderboards. - Player & Score Tracking: Comprehensive data structures to track player sessions, active/completed themes, and dynamic leaderboard rankings.
Computer-Network-Project/
├── bin/ # Compiled object files (*.o)
├── const/
│ └── const.h # Global constants, macros, and configuration paths
├── include/
│ ├── all.h # Standard library and POSIX networking headers
│ ├── client.h # Client-specific definitions
│ ├── serverlib.h # Data structures and server library prototypes
│ └── serverlib.c # Implementation of file loading, linked lists, and game logic
├── src/
│ ├── client.c # Client application entry point
│ └── server.c # Server application entry point and connection listener
├── txt/ # Quiz database (categories, questions, and answers)
│ ├── temi.txt # List of available trivia themes
│ ├── domande1.txt # Questions for Theme 1
│ ├── risposte1.txt # Answers for Theme 1
│ ├── domande2.txt # Questions for Theme 2
│ └── risposte2.txt # Answers for Theme 2
├── makefile # Build automation script
└── README.md # Project documentation
The application relies on several core data structures defined in include/serverlib.h:
Represents a trivia category.
name: Title of the category (loaded fromtemi.txt).NUM_DOMANDE: Number of questions in the category.lista_domande&lista_risposte: Singly linked lists (NODE*) containing the questions and corresponding answers.mutex: Dedicated mutex to ensure thread-safe score updates and leaderboard access for the theme.
Represents a connected user session.
name: Player username (up toNAME_MAXcharacters).active: Boolean flag indicating if the player is currently connected.temi_iniziati&temi_finiti: Boolean tracking arrays for theme progress.next: Pointer to the next player in the global connected players list.
A dynamic linked list node used for storing variable-length quiz content in memory without hardcoded array limits.
To compile and run this project, you need a POSIX-compatible environment (Linux, macOS, or Windows via WSL/MSYS2) with:
- GCC (GNU Compiler Collection)
- Make
- POSIX Threads & Sockets support
A makefile is provided for easy build automation. Open your terminal in the project root directory and run:
# Compile both server and client executables
make
# Or compile individually:
make server
make clientTo clean up compiled binaries and object files:
make clsStart the server first. It will bind to 127.0.0.1:3456, load the trivia questions from the txt/ directory into memory, and begin listening for client connections:
./serverExpected output:
Server in ascolto
In a separate terminal window (or multiple windows for concurrent players), launch the client:
./clientThis project is currently in active development as part of a university coursework submission:
- Server Core: Socket creation, TCP binding, listening, and accept loop.
- Concurrency: Worker thread creation per connected client with mutex initialization.
- Data Persistence & Parser: Robust text file parser stripping whitespace and loading questions/answers into linked lists.
- Game Loop & Protocol: Implementing full client-server message exchange inside
th_client. - Client CLI: Interactive terminal user interface for selecting themes and answering questions.
- Leaderboard System: Finalizing score calculation and ranking algorithms (
ord&aggiornaPunteggio).
Danilo Parisi
- GitHub: @DaniloParisi03