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

The current version of the server that I have here can serve only one client. Ex

ID: 3836186 • Letter: T

Question

The current version of the server that I have here can serve only one client. Extend it to be the one that supports multiple clients. For example, when the server and two clients concurrently run, one client will receive message “Hello World 1” and another client will receive message “Hello World 2”. To do so, you need to use Threads when writing the server.

Server.java

/*** Simulated Socket operation in server side ***/

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

public class Server{

public final static int port = 31; //To set up Port 31 as a communication port, you can modify this value
  
public static void main (String[] args){
  
//To declare Socket variable in server side
ServerSocket serverSocket;
   Socket socket;
      
   PrintWriter output; //To output strings
      
   //To simulate server waiting for request from client side
   try{
   serverSocket = new ServerSocket(port);
//To set up connected port for Socket in server side
          
   try{
   while(true){
//Endless loop, "Ctrl + c" can terminate this program

       socket = serverSocket.accept();
//To wait for connection from client side
              
       //To output strings to client side
       output = new PrintWriter(socket.getOutputStream());

//To get request message from the client
       output.println("Hello World");
              
   output.flush(); //To write out data from buffer
   socket.close(); //To close Socket connection
           }
}
catch(IOException e){
   serverSocket.close();
//To close Socket in the server side
   System.err.println(e);
       }          
   }
   catch(IOException e){
       System.err.println(e);
       }
}
}

Client.java

/*** Simulated Socket operation in client side ***/

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

class Client{

//To simulate client side gets service from server side
   public void getService(String destination, int port){
  
       try{
       Socket socket = new Socket(destination, port); // To declare Socket variable in client side
          
           //To print string which gets from server side
           InputStream input = socket.getInputStream();
          
           for(int charInput = input.read();charInput > 0; charInput = input.read())
           System.out.print((char)charInput);
          
           socket.close(); //To close Socket connection  
       }
       catch(IOException e){
       e.printStackTrace();
       }
   }
  
   public static void main (String[] args){
  
       String serverAddress = args[0]; //For input IP address of server side when compile Client.class
       System.out.println(serverAddress);
      
       //To run client side
       Client client = new Client();
       client.getService(serverAddress, 31); //We set up port 31 provide connection in server side
   }

}

HostInformation.java

/*** To get host name and IP address ***/

import java.net.InetAddress;

class HostInformation{
  
   public static void main(String[] args){
  
       try{
       InetAddress host = InetAddress.getLocalHost(); //To declare a InetAddress object to represent host
          
           System.out.println("Host Name: " + host.getHostName()); //To get host name
           System.out.println("IP Address: " + host.getHostAddress()); //To get IP address  
       }
       catch(Exception e){
       e.printStackTrace();
       }
   }
}

Explanation / Answer

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

public class Server{

public final static int port = 31;
  
public static void main (String[] args){
  
ServerSocket serverSocket;
   Socket socket;
      
   PrintWriter output; //To output strings
      
try{
   serverSocket = new ServerSocket(port);   try{
   while(true){
socket = serverSocket.accept();
output = new PrintWriter(socket.getOutputStream());

output.println("Hello World");
              
   output.flush();
   socket.close();
           }
}
catch(IOException e){
   serverSocket.close();
System.err.println(e);
       }          
   }
   catch(IOException e){
       System.err.println(e);
       }
}
}

Client.java

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

class Client{

public void getService(String destination, int port){
  
       try{


       Socket socket = new Socket(destination, port); InputStream input = socket.getInputStream();
          
           for(int charInput = input.read();charInput > 0; charInput = input.read())
           System.out.print((char)charInput);
          
           socket.close();
       }
       catch(IOException e){
       e.printStackTrace();
       }
   }
  
   public static void main (String[] args){
  
       String serverAddress = args[0];
       Client client = new Client();
       client.getService(serverAddress, 31);
   }

}

HostInformation.java

import java.net.InetAddress;

class HostInformation{
  
   public static void main(String[] args){
  
       try{
       InetAddress host = InetAddress.getLocalHost();
           System.out.println("Host Name: " + host.getHostName());
           System.out.println("IP Address: " + host.getHostAddress());
       }
       catch(Exception e){
       e.printStackTrace();
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote