Question
Weve looked at how to create a Client/Server connection, how to send data, and how to host multiple Clients. Now were going to put everything
Weve looked at how to create a Client/Server connection, how to send data, and how to host multiple Clients. Now were going to put everything together by creating a program in which two users can interact with each other.
First, the setup. Create a Server and a Client. The Server will accept two Clients when it starts. Then, were going to assign an ID to both Clients. Once we have this setup, the Server is going to play a guessing game with the Clients. The Server will randomly pick a number between 1 and 10, and then it will get a guess from each Client. Each Client will accept a numeric entry between 1 and 10 from the user and send their entry to the Server. Dont forget to use Data Validation and ask for a new entry without terminating the program if invalid data is entered. When the Server has received both entries, it will determine a winner and send a confirmation to both Clients. Everything will then finish and disconnect.
While this may sound complex, there are several ways to keep this program simple. Once both IDs have been sent, get an integer from each Client and have the Server receive them one at a time. Then, compare both numbers to the chosen number and decide which player won or if it is a tie (in the case that both players choices are the same distance from the servers chosen number i.e. if the Server chose 5 and the users chose 3 and 7). Create a String that describes the outcome of the game like shown below and send that to both Clients. This will be enough to finish execution.
Player 1 chose 2. Player 2 chose 7. The Server chose 3. Player 1 wins!
To summarize, your program should do the following:
Create a Server class and a Client class. Have two Clients connect to the Server.
Have the Server send an ID to both Clients.
Have the Server choose a random number from 1 to 10 for a guessing game.
Allow both Clients to get user-input in the form of a number.
Perform entry validation to ensure that each entry is an integer between 1 and 10, prompting for a re-entry without terminating the program if the input is invalid.
Send both user entries back to the Server.
Determine which user chose a number closest to the Servers target number.
Send a String with an outcome message to both Clients.
Disconnect and close everything.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started