Skip to content

AbdallahSHILI/JavaSocket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JavaSocket

Client-Server Number Guessing Game

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.

Prerequisites

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)

Getting Started

  1. Clone the repository or download the source code files (ClientSide.java and ServerSide.java).

  2. Compile both the ClientSide.java and ServerSide.java files using the following commands:

    javac ClientSide.java
    javac ServerSide.java
    
  3. Start the server by running the ServerSide class:

    java ServerSide
    

    The server will start and wait for a client connection.

  4. 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.

  5. Enter a number between 0 and 20 on the client side and press Enter. The client will send the number to the server.

  6. 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.

  7. Continue guessing numbers on the client side until you correctly guess the random number generated by the server.

  8. Once the correct number is guessed, the server will send a message indicating that you won and display the random number generated.

  9. The client and server will automatically close the connection, and the game will end.

Code Explanation

ClientSide.java

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.

ServerSide.java

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.

Conclusion

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.

Team Members

Abdallah Shilli, @AbdallahSHILI

Ahmed Elj, @lorffine

Zied Jarboui

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages