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.
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(); } } }
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