Skip to content

SutejUpadhyaya/Client-Server-Trivia

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Client–Server Trivia Game (C)

This project is a two-player trivia game implemented in C using TCP sockets. One program runs as a server and coordinates the game, while two clients connect, receive trivia questions, submit answers, and compete for the highest score.

The server enforces basic synchronization by waiting for both players to answer each question before updating scores and advancing the game. All trivia content is read from a file, making the game data-driven and easy to extend without modifying source code.

The project is intentionally terminal-based and lightweight, focusing on core systems programming concepts such as socket communication, blocking I/O, and client–server coordination.


Project Structure

server.c
Implements the trivia server. Sets up a TCP socket, accepts exactly two client connections, reads questions from a file, receives answers from both players, checks correctness, and maintains player scores.

client.c
Implements the trivia client. Connects to the server, sends a player name, displays questions received from the server, submits answers, and prints score updates.

questions.txt
Primary trivia question file used by the server.

questionss.txt
Alternate trivia question file using the same format.

Makefile
Builds the server and client programs.


Building the Project (From Scratch)

Requirements:

  • A Unix-like environment (macOS or Linux)
  • gcc installed

Steps:

git clone https://github.com/SutejUpadhyaya/Client-Server-Trivia.git
cd Client-Server-Trivia
make

After building, two executables will be created:

server
client


Running the Game (Quick Start)

Start the server

Open a terminal and run:

./server

The server will start listening and wait until two players connect.


Start the clients

Open two separate terminals (or use two different machines on the same network) and run:

./client

Each client will:

  1. Prompt for a player name
  2. Receive trivia questions from the server
  3. Submit answers
  4. Receive score updates after both players answer

The game continues until the question file is exhausted.


Running Across Multiple Machines

To run the game across different computers:

  1. Start the server on one machine.
  2. Update the server IP address in client.c if needed.
  3. Build the project on each machine using make.
  4. Run the server on one machine and the clients on two machines connected to the same network.

Question File Format

Each trivia question consists of four parts:

  1. One line containing the question
  2. One line containing three possible answers separated by spaces
  3. One line containing the correct answer (must exactly match one of the options)
  4. A blank line separating questions

Example:

What is the capital of France?
Berlin Madrid Paris
Paris


About

A two-player client–server trivia game written in C that demonstrates TCP socket programming, basic synchronization, and file-driven game logic.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors