Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

First, the setup. Create a Server and a Client. The Server will accept two Clien

ID: 3817759 • Letter: F

Question

First, the setup. Create a Server and a Client. The Server will accept two Clients when it starts. Then, we’re 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. Don’t 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 server’s 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 Server’s target number.

Send a String with an outcome message to both Clients.

Disconnect and close everything.

Explanation / Answer

Server.java

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.Random;

public class Server {

    private ObjectInputStream input;

    private ObjectOutputStream output;

    private Socket connection;

    private ServerSocket server;

    private int counter = 0;

    public void startRunning() {

        try {

            server = new ServerSocket(53);

            while (true) {

              waitForConnection();

            }

        } catch (Exception e) {

        }

    }

    public void waitForConnection() throws IOException {

        System.out.println("waiting for connection.... ");

        connection = server.accept();

        new Thread(new Worker()).start();

        counter++;

        System.out.println("connection" + counter + "received from:" + connection.getInetAddress().getHostName());

    }

    public class Worker implements Runnable {

        private int guess;

        private final int target;

        private final Random ran;

        public Worker() {

            ran = new Random();

            target = ran.nextInt(10) + 1;

        }

        public void processConnection() {

            try {

                guess = input.readInt();

                System.out.println("client guessed-" + guess);

            } catch (Exception e) {

            }

        }

        public void setUpStreams() throws IOException {

            output = new ObjectOutputStream(connection.getOutputStream());

            output.flush();

            input = new ObjectInputStream(connection.getInputStream());

        }

        public void askForGuess() throws IOException {

            sendMessage("please type in a guess ");

            System.out.println("client guessed:" + guess);

        }

        public void checkGuess() throws IOException {

            while (guess != target) {

                sendMessage("you guesses wrong,please try again ");

                askForGuess();

            }

            sendMessage("you win! ");

        }

        public void sendMessage(String message) throws IOException {

            output.writeObject(message);

            output.flush();

        }

        @Override

        public void run() {

            try {

                setUpStreams();

                processConnection();

                askForGuess();

                checkGuess();

            } catch (IOException e) {

                //todo Auto-generated catch block

                e.printStackTrace();

            }

        }

    }

    public static void main(String args[]) {

        Server s = new Server();

        s.startRunning();

    }

}

Client.java

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.net.InetAddress;

import java.net.Socket;

import java.util.Scanner;

public class Client {

    //private int guess;

    Scanner scan;

    private Socket connection;

    private ObjectOutputStream output;

    private ObjectInputStream input;

    public Client() {

        scan = new Scanner(System.in);

    }

    public void startRunning() {

        try {

            ConnectToServer();

            setUpStreams();

            processConnection();

        } catch (Exception e) {

        }

    }

    public void ConnectToServer() {

        try {

            System.out.println("Attempting connection...");

            connection = new Socket(InetAddress.getByName("localhost"), 53);

        } catch (Exception e) {

            System.out.println("cannot find server");

        }

    }

    public void setUpStreams() throws IOException {

        output = new ObjectOutputStream(connection.getOutputStream());

        output.flush();

        input = new ObjectInputStream(connection.getInputStream());

        System.out.println("Got streams");

    }

    public void processConnection() {

        try {

            String message = "you are now connected";

            System.out.println(message);

        } catch (Exception e) {

        }

    }

    public void sendGuess(int guess1) throws IOException {

        output.writeInt(guess1);

        output.flush();

    }

    public static void main(String args[]) {

        Client c = new Client();

        c.startRunning();

    }

}