-1

I have a question regarding server/client running on the command line. The server should be run something like this

Server should run with a command line passing port number

java Server port_number

Client should run with command line as following format:

java Client serverIP server_port_number commandFile

I was wondering if someone could show me an example of what the beginning of the "main method" should look like in both server/client to properly satisfy/take in these arguments when run on the command line.

Cœur
  • 37,241
  • 25
  • 195
  • 267
j doe
  • 83
  • 1
  • 7

1 Answers1

1
class ServerExample{  
  public static void main(String args[]){  
    System.out.println("Your first argument is: "+args[0]);  
    int serverPort = Integer.parseInt(args[0]);
  }  
}

This will print port_number (as mentioned in the server execution).

class ClientExample{  
  public static void main(String args[]){  
    System.out.println("Your first argument is: "+args[0]);
    System.out.println("Your second argument is: "+args[1]);
    System.out.println("Your third argument is: "+args[2]);
    String serverIP = args[0];
    int serverPort = Integer.parseInt(args[1]);
    String commandFile = args[2];
  }  
}

This will print serverIP, server_port_number, and commandFile (as mentioned in the client execution).

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73