Search code examples
javacommand-lineclient-servercommand-line-argumentsmain-method

Server/Client Running with command line


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.


Solution

  • 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).