Question
import java.io.*; import java.net.*; public class DJClient { private static InetAddress host; public static void main(String[] args) { try { // Get server IP-address host
import java.io.*; import java.net.*;
public class DJClient { private static InetAddress host; public static void main(String[] args) { try { // Get server IP-address host = InetAddress.getByName(args[0]); } catch(UnknownHostException e){ System.out.println("Host ID not found!"); System.exit(1); } run(Integer.parseInt(args[1])); }
private static void run(int port) { Socket link = null; try{ // Establish a connection to the server link = new Socket(host,port);
// Set up input and output streams for the connection BufferedReader in = new BufferedReader( new InputStreamReader(link.getInputStream())); PrintWriter out = new PrintWriter( link.getOutputStream(),true); //Set up stream for keyboard entry BufferedReader userEntry = new BufferedReader(new InputStreamReader(System.in)); String message, response;
// Get data from the user and send it to the server do{ System.out.print("Enter message: "); message = userEntry.readLine(); out.println(message); }while (!message.equals("DONE"));
// Receive the final report and close the connection response = in.readLine(); System.out.println(response); } catch(IOException e){ e.printStackTrace(); }
finally{ try{ System.out.println(" !!!!! Closing connection... !!!!!"); link.close(); }
catch(IOException e){ System.out.println("Unable to disconnect!"); System.exit(1); }
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.*; import java.net.*;
public class DJServer { private static ServerSocket servSock; public static void main(String[] args) { System.out.println("Opening port... "); try{
// Create a server object servSock = new ServerSocket(Integer.parseInt(args[0])); }
catch(IOException e){ System.out.println("Unable to attach to port!"); System.exit(1); }
do { run(); }while (true);
}
private static void run() { Socket link = null; try{
// Put the server into a waiting state link = servSock.accept();
// Set up input and output streams for socket BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream())); PrintWriter out = new PrintWriter(link.getOutputStream(),true);
// print local host name String host = InetAddress.getLocalHost().getHostName(); System.out.println("Client has estabished a connection to " + host);
// Receive and process the incoming data int numMessages = 0; String message = in.readLine(); while (!message.equals("DONE")) { System.out.println(message); numMessages ++; message = in.readLine(); }
// Send a report back and close the connection out.println("Server received " + numMessages + " messages"); }
catch(IOException e){ e.printStackTrace(); }
finally{ try{ System.out.println("!!!!! Closing connection... !!!!! " + "!!! Waiting for the next connection... !!!"); link.close(); }
catch(IOException e){ System.out.println("Unable to disconnect!"); System.exit(1); } }
}
}
Client-Server code needs client program to accept three optional command line arguments, modify client program to take 3 optional comman line arguments username, server host address and port #. If the user name is not given on the command line, the client programneeds to ask the user for the username . If the server host address is not given on the command line, the client will assume the server is running on the local host. The client program will use a default port which is hard coded if the port number is not given on the command line. Here are the client and server programs that need to be changedStep 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