Java 当我运行客户机-服务器程序时,我得到ArrayIndexOutofBoundException

Java 当我运行客户机-服务器程序时,我得到ArrayIndexOutofBoundException,java,networking,client-server,Java,Networking,Client Server,我正在编写我的第一个java客户机/服务器程序,它只是建立了与服务器的连接,当我运行程序时,应该打印IP地址和端口号。但当我运行服务器程序时,我得到一个错误 Server.java package serverpro; import java.io.*; import java.net.*; public class Server{ static InetAddress ip; public static final String HOST="localhost"; public sta

我正在编写我的第一个java客户机/服务器程序,它只是建立了与服务器的连接,当我运行程序时,应该打印IP地址和端口号。但当我运行服务器程序时,我得到一个错误

Server.java

package serverpro;
import java.io.*;
import java.net.*;

public class Server{
 static InetAddress ip;
 public static final String HOST="localhost";
 public static final int PORT= 4444; 


public static void main(String a[]) throws Exception {
System.out.println("starting server..");
System.out.println("Initializing Connection..");

try (
        ServerSocket serverSocket = new ServerSocket(PORT);
        Socket clientSocket = serverSocket.accept();

        //BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        //PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
) {
    ip = InetAddress.getLocalHost();
    System.out.println("InetAdress " + ip.getHostAddress() + " : " + clientSocket.getPort());


} catch (Exception e) {
    System.err.println("Exception in starting server: " + e.getMessage());
}
}
 }
Client.java

package serverpro;

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

 public class Client {
 public static void main(String[] args) throws IOException {
try (
        Socket client = new Socket(Server.HOST, Server.PORT);
        PrintWriter out = new PrintWriter(client.getOutputStream(), true);

        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
) {
    String inputLine;
    while ((inputLine = stdIn.readLine()) != null) {

        if ("exit".equals(inputLine)) {
            out.println("exit");
            break;
        }

        out.println(inputLine);
        out.flush();

        final String response = in.readLine();
        System.out.println(response);
    }
} catch (UnknownHostException e) {
    System.err.println("Don't know about host: localhost.");
    System.exit(1);
} catch (IOException e) {
    System.err.println("Couldn't get I/O for the connection to: localhost.");
    System.exit(1);
}
} }

现在,当我首先运行ServerProgram时,我得到了下面的输出

运行:

但当我运行客户端输出时,它显示为null


运行:

我猜您启动程序时没有任何参数,因此args数组为空。使用参数启动程序,或找到其他方法获取服务器名称和端口。

试试这个

java -cp /path/toCompiled/code greetingserver.GreetingClient serverName 8080

应该在端口8080上打印连接到服务器名我没有从您的描述中得到任何信息,但是如果您希望服务器回复您将写入命令提示符的内容,您应该更改in.readLine

 while (x) {
        reply= stdIn.readLine();
        if(reply!=null)
        {
            x=false;
        }
    }
另外,如果您想打印ip地址和端口,您应该打印它,lol

System.out.println("InetAdress " + echoSocket.getInetAddress() + ":" + echoSocket.getPort());
检查这个

public class Server {
    public static final String HOST="localhost";
    public static final int PORT= 4444;  

public static void main(String a[]) throws Exception {
    System.out.println("starting server..");
    System.out.println("Initializing Connection..");
    try (
            ServerSocket serverSocket = new ServerSocket(PORT);
            Socket clientSocket = serverSocket.accept();

            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
    ) {
        System.out.println("start");
        System.out.println("client: " + clientSocket.getInetAddress().getHostAddress());

        while (true) {
            System.out.println("waiting request...");
            final String request = in.readLine();

            if ("exit".equals(request)) {
                System.out.println("exit command");
                break;
            }
            System.out.println("received: " + request);
            final String response = "server:" + request;
            out.println(response);

            System.out.println("sent back: " + response);
        }

    } catch (Exception e) {
        System.err.println("Exception in starting server: " + e.getMessage());
    }
}
}



}

你能给我建议一种无参数自动启动的方式吗?@poojagajera使用属性文件。不,我不明白。请你写一段代码让我明白。谢谢HII我需要做什么更改才能用参数启动程序。我在代码中做了更改请参见上面的代码。但我仍然没有得到预期的输出–可能您在运行时没有将portcommand line arg传递给GreetingServer。请您写出整行代码,以便我了解您的想法。如何使用端口号写入?如何运行它?通过命令行还是某个IDE?我正在运行IDE。当我运行程序时,端口号和IP地址应自动生成,无需输入任何内容manually@poojagajera如果要自动分配端口号,为什么要编写从参数中获取端口号的代码?当我运行程序时,端口号和IP地址应自动分配,而无需通过cmd写入。它应该只在IDE中运行。我对代码进行了更改,请参见上面的代码。但我仍然没有得到预期的输出–Hiii当我运行程序时,我应该自动获得ip地址和端口号,而无需手动输入任何内容。这在这里不起作用。当我运行我的程序时。我应该得到服务器名和端口号。我不需要手动输入,这是什么意思?通过程序参数传递主机/端口?当我运行服务器程序时,我应该自动获取IP地址和端口号。当我运行服务器程序时,我应该获取计算机的IP地址和端口号。我需要在哪个文件中进行此更改。服务器中的服务器或客户端。在BufferedReader stdIn声明之后。你把你的in-bufferedReader和stdIn-bufferedReader弄混了,我猜。你能不能写一整段代码,这样我就能更好地理解我和你共享git-hub链接?嗨,我对代码做了更改,请再次查看上面的代码。
public class Server {
    public static final String HOST="localhost";
    public static final int PORT= 4444;  

public static void main(String a[]) throws Exception {
    System.out.println("starting server..");
    System.out.println("Initializing Connection..");
    try (
            ServerSocket serverSocket = new ServerSocket(PORT);
            Socket clientSocket = serverSocket.accept();

            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
    ) {
        System.out.println("start");
        System.out.println("client: " + clientSocket.getInetAddress().getHostAddress());

        while (true) {
            System.out.println("waiting request...");
            final String request = in.readLine();

            if ("exit".equals(request)) {
                System.out.println("exit command");
                break;
            }
            System.out.println("received: " + request);
            final String response = "server:" + request;
            out.println(response);

            System.out.println("sent back: " + response);
        }

    } catch (Exception e) {
        System.err.println("Exception in starting server: " + e.getMessage());
    }
}
public class Client {

public static void main(String[] args) throws IOException {
    try (
            Socket client = new Socket(Server.HOST, Server.PORT);
            PrintWriter out = new PrintWriter(client.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    ) {
        String inputLine;
        while ((inputLine = stdIn.readLine()) != null) {

            if ("exit".equals(inputLine)) {
                out.println("exit");
                break;
            }

            out.println(inputLine);
            out.flush();

            final String response = in.readLine();
            System.out.println(response);
        }
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: localhost.");
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: localhost.");
        System.exit(1);
    }
}