A simple client-server number guessing game built with Java UDP sockets (DatagramSocket / DatagramPacket).
The server picks a secret number. The client sends up to 5 guesses, and the server replies after each guess to say whether the secret number is higher, lower, or if the client guessed correctly.
- Server (
GuessServer) listens on UDP port7000. - Client (
Player) listens on UDP port8000and sends guesses to the server. - The server compares each guess to the secret number and replies with one of:
Plus grand— the secret number is greater than the guessPlus petit— the secret number is smaller than the guessGagne— the client guessed correctly (server then stops)
- The client stops early once it receives
Gagne, otherwise it gets up to 5 attempts.
udp-number-guessing-game/
├── client/
│ └── src/com/game/client/Player.java
└── server/
└── src/com/game/server/GuessServer.java
Both programs assume localhost (127.0.0.1), so run them on the same machine in two terminals.
1. Compile:
javac -d server/bin server/src/com/game/server/GuessServer.java
javac -d client/bin client/src/com/game/client/Player.java2. Start the server first (it needs the secret number before the client sends guesses):
java -cp server/bin com.game.server.GuessServerEnter the secret number when prompted.
3. Start the client:
java -cp client/bin com.game.client.PlayerEnter a guess when prompted, for up to 5 attempts.
This project was originally written with French variable/message names (joueur, serveurG, Plus grand/Plus petit) as part of a networking course exercise, then refactored for clarity and to fix a couple of small bugs (a double-increment in the client's loop, and reading stray null bytes from the receive buffer).
- Add input validation (non-numeric input currently crashes the program)
- Make the host/port configurable via command-line arguments
- Add a
Timeouton the client socket so it doesn't hang forever waiting for a reply - Package as a Maven/Gradle project instead of raw
javaccompilation