-1

我有一个关于在命令行上运行的服务器/客户机的问题。该服务器应运行像这样使用命令行运行的服务器/客户机

服务器应该使用命令行运行通过端口号

Java服务器端口号

客户用命令行下面的格式应该运行:

Java客户端服务器IP server_port_number commandFile

我在想,如果有人能告诉我一个例子,“主要方法”在服务器/客户端应该看起来像什么,以正确满足/在命令行上运行时接受这些参数。

回答

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

这将打印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]; 
    } 
} 

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

+0

我可以知道为什么downvote? –

+0

o我感谢您的帮助。顺便说一句,我不认为我downvoted。我现在将测试这个编辑:嘿感谢它完美的工作 –

相关问题