服务器/客户端使用命令行运行

时间:2022-04-19 08:11:10

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

java服务器port_number

Client should run with command line as following format:

客户端应使用命令行运行,格式如下:

java Client serverIP server_port_number commandFile

java客户端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.

我想知道是否有人可以向我展示一个示例,说明在主服务器/客户端中“主方法”的开头应该是什么样,以便在命令行上运行时正确满足/接受这些参数。

1 个解决方案

#1


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

这将打印port_number(如服务器执行中所述)。

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

这将打印serverIP,server_port_number和commandFile(如客户端执行中所述)。

#1


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

这将打印port_number(如服务器执行中所述)。

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

这将打印serverIP,server_port_number和commandFile(如客户端执行中所述)。