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

import java.io.*; import java.net.*; public class DJClient { private static Inet

ID: 3880598 • Letter: I

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 = 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 changed.

Explanation / Answer

here is your modified program : ----------------->>>>>>>>>>>>..

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

public class DJClient
{
    private static InetAddress host;
private static String user;
    public static void main(String[] args)
   {
       try {
// Get server IP-address
    if(args[0] != null){
     user = args[0];
    }else{
     Scanner sc = new Scanner(System.in);
     System.out.print(" Enter the User name : ");
     user = sc.next();
    }
                  host = InetAddress.getByName(args[1]);
       }
       catch(UnknownHostException e){
           host = InetAddress.getLocalHost();
       }
       try{
   int n = Integer.parseInt(args[2]);
   run(n);
  }catch(Exception e){ run(3450);}
    }
    private static void run(int port)
    {
        Socket link = null;
        try{
        // Establish a connection to the server
                 link = new Socket(host,port);
    System.out.println("USER : "+user);
        // 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){
             servSock = new ServerSocket(3450);
        }
       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);
           }
    }
}
}