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

For this activity, you will submit two .java files. You will need to run two cla

ID: 3818780 • Letter: F

Question

For this activity, you will submit two .java files. You will need to run two classes simultaneously for this activity.

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.

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

// run the client program twice in seprate window for multiple client or player..

/*
Client side program... for multiple clients, run this program in seperate windows.
*/

import java.io.*;
import java.net.*;

public class clientside implements Runnable{
{
  

public static void main(String args[]) throws Exception
{

System.out.println("connection to server....");

try
{
     
static Socket clientSocket = new Socket("xyz",1010);
static PrintStream out = new PrintStream(clientScocket.getOutputStream());
static BufferedReader in =new BufferedReader(new InputStreamReader(clientSocket.getInputStream());
static BufferedReader userin = new BufferedReader(new InputStreamReader(System.in));
static Flag=false;
}


catch(Exception e)
{
System.out.println("Error:"+e.getMessage());
}

if(clientSocket != null)
{
try
  
{
new thread(new clientside()).start(); // client side thread starts here
  
while(!Flag)

{
   System.out.println(" "+in.readLine());

int n= Integer.parseInt(userin.readLine());
   if(n>0&& n<11)                // check for validation
   out.println(n);
   else
   System.out.println(" Please enter a valid number between 1 to 10:");
}

out.close();
   in.close();
   clientSocket.close(); // close the client socket

    }

  
catch(Exception e)
{
System.out.println("Error:"+e.getMessage());
}
  
}   
}

public void run()
{
String ID;

try
{
while((ID=in.readLine())!= null)
System.out.println("user ID:" +ID);
Flag= true; //session ends

}


catch(Exception e)
{
System.out.println("Error:"+e.getMessage());
}
  
}   
}



/* Server Side program */

import java.io.*;
import java.net.*;
import java.util.Random;

public class ServerSide
{

static Socket cs= null; // client socket to listen

static clientThread ct[] = new clientThread[3];
,


public static void main(String arg[]) throws Exception
{

system.out.println("Server side is active....");

try
   {
   static ServerSocket ss =new ServerSocket(1010); // server socket to connect with client
  


   catch(Exception e)
{
System.out.println("Error:"+e.getMessage());
}


}
while(true)
{

try
{

cs=ss.accept();
for(int i=0;i<2;i++)
{
   if(ct[i]==null)
   {
   (ct[i]= new clientThread(cs,ct)).start();break;
   }
}


catch(Exception e)
{
System.out.println("Error:"+e.getMessage());
}
}   

}

}

class clientThread extends Thread
{
   static Socket cs = null;
clientThread ct[];

public clientThread(Socket cs,clientThread[] ct)
{
this.cs=cs;
this.ct=ct;
}


public void run()
  
{
   Random rand = new Random();

int id = rand.nextInt(9999) + 1111;
   int n= rand.nextInt(10)+1;
  
String name;

   try
   {

   int j=0; int[] no=new no[2];

static PrintStream out = new PrintStream(cs.getOutputStream());
static BufferedReader in =new BufferedReader(new InputStreamReader(cs.getInputStream());
   out.println("your Unique Id :" + id);
   out.println("Enter a number between 0 to 10 :");
  
     
      
for(int i=0;i<2;i++)
   {
   if(ct[i]!= null && ct[i]!=this){
      
   no[i]= Integer.parseInt(in.readLine());
System.out.println("player"+i+"chose +":" + no[i] );
       if(n==no)
   System.out.println("player"+ +":" + wins );
   }
      
}

   int distance = Math.abs(n0[0] - n);
int idx = 0;
for(int c = 1; c < 2; c++){
int cdistance = Math.abs(no[c] - n);
if(cdistance < distance){
idx = c;
distance = cdistance;
}
}

   System.out.println("server chose:" + n );
   System.out.println("player"+idx+"wins!" );

in.close();
   out.close();
   cs.close();
   }

catch(Exception e)
{
System.out.println("Error:"+e.getMessage());
}
}   

}


     
OUTPUT:

server side:

player 1 chose :2  
player 2 chose :9

server chose :4

Player 1 wins!
  

client 1:

Id : 1234

Enter the number beween 1 to 10: 2

client 2:

Id : 3245

Enter the number beween 1 to 10: 32

please enter a valid number between 1 to 10: 9