This is a simple client-server application implemented in Java that allows a client to guess a random number generated by the server. The client sends guesses to the server, and the server responds with hints indicating whether the guess is higher or lower than the random number. The game continues until the client guesses the correct number.
To run this application, you need to have the following:
- Java Development Kit (JDK) installed on your machine
- A Java IDE or a text editor to modify the code (optional)
-
Clone the repository or download the source code files (
ClientSide.java
andServerSide.java
). -
Compile both the
ClientSide.java
andServerSide.java
files using the following commands:javac ClientSide.java javac ServerSide.java
-
Start the server by running the
ServerSide
class:java ServerSide
The server will start and wait for a client connection.
-
In a separate terminal or command prompt window, start the client by running the
ClientSide
class:java ClientSide
The client will connect to the server and prompt you to enter a number between 0 and 20.
-
Enter a number between 0 and 20 on the client side and press Enter. The client will send the number to the server.
-
The server will compare the received number with the randomly generated number and send back a response indicating whether the guess is correct, higher, or lower.
-
Continue guessing numbers on the client side until you correctly guess the random number generated by the server.
-
Once the correct number is guessed, the server will send a message indicating that you won and display the random number generated.
-
The client and server will automatically close the connection, and the game will end.
The ClientSide
class represents the client-side of the application. It establishes a connection to the server using a Socket
and communicates with the server using input and output streams.
The client prompts the user to enter a number between 0 and 20 and sends it to the server. It then waits for a response from the server and displays the response to the user. The client continues to prompt for guesses until the correct number is guessed.
The ServerSide
class represents the server-side of the application. It creates a ServerSocket
on port 3000 and waits for a client connection.
When a client connects, the server generates a random number between 0 and 20. It then enters a loop to receive guesses from the client. The server compares each received guess with the randomly generated number and sends back a response indicating whether the guess is correct, higher, or lower.
The server continues to receive guesses until the correct number is guessed or the client closes the connection. Once the game ends, the server closes the socket and the server socket.
This client-server number guessing game demonstrates the basic communication between a client and a server using sockets in Java. The client sends guesses to the server, and the server responds with hints until the correct number is guessed. Feel free to modify and enhance the code to add more features or improve the user experience.
Abdallah Shilli, @AbdallahSHILI
Ahmed Elj, @lorffine
Zied Jarboui