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: 3809588 • Letter: F

Question

For this activity, you will submit two .java files. You will need to run two classes simultaneously for this activity. Make sure you have a compiler that is capable of this.

Now that you’ve learned how to connect multiple Clients to a single Server, we’re going to look at how this affects data being sent back and forth.

For this program, you’re going to write a single Client class and a Server class. Your Server class should accept two connections – one for each Client (you’ll run your Client class twice like before as well). This time, after both Clients connect, your Server will send them both an ID like last time. Once a Client has gotten an ID, the Client should ask the user input for a name to go with their ID. The Client will enter their name and send it back to the Server. The Server will connect both names and print out the ID-name combinations like this:

ID1: Name1
ID2: Name2

Finally, the Server will finish and disconnect. The Clients should also disconnect. Both sides should close everything and finish running.

Note: Both clients must connect to the Server before the IDs are sent. They must both be connected to the Server simultaneously.

To summarize, your program should do the following:

Create a Server class with two Sockets and one ServerSocket.

Create a Client class. This class will connect to the Server once.

Have the Server accept two Client connections.

Send unique IDs to each Client.

Have each Client get a String name (or username) from their user.

The Client sends their name (or username) to the Server.

The Server reads the names from both Clients and displays them with their IDs.

Explanation / Answer

/*

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)

{

out.println(userin.readLine());

}

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;

  

String name;

try

{

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 your name:");

name= in.readLine();

for(int i=0;i<2;i++)

{

if(ct[i]!= null && ct[i]!=this)

System.out.println(id +":" + name);

}

in.close();

out.close();

cs.close();

}

catch(Exception e)

{

System.out.println("Error:"+e.getMessage());

}

}   

}