0

我正在实现一个服务器类。在此,当我尝试将 hostName 和 portNumber 作为命令行参数传递时,我得到了 ArrayIndexOutOfBound 异常。

import java.net.*;
import java.io.*;

public class EchoServer{
public static void main(String[] args) throws IOException{
    String hostName = args[0];
    int portNumber = Integer.parseInt(args[1]);

if(args.length!=2){
System.err.println("Usage: java EchoClient <hostName> <portNumber>");
System.exit(1);

}



try(
    Socket echoSocket = new Socket(hostName,portNumber);
    PrintWriter out = new PrintWriter(echoSocket.getOutputStream(),true);
    BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

) {
    String userInput; 
    while((userInput = stdIn.readLine())!=null){
        out.println(userInput);
        System.out.println("echo: " +in.readLine());
    }
}   catch (UnknownHostException e) {
            System.err.println("Don't know about host " + hostName);
            System.exit(1);
     }  catch (IOException e) {
         System.err.println("Couldn't get I/O for the connection to " +
            hostName);
         System.exit(1);
 }
}

}

4

0 回答 0