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

时间:2017-02-06 06:33:57

标签: java networking client-server

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

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时,我得到了输出

运行:

Entered server console..
Initializing Connection..

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

运行:

4 个答案:

答案 0 :(得分:0)

我猜你没有用任何参数启动程序,所以你的args数组是空的。使用参数启动程序或找到获取服务器名称和端口的不同方法。

答案 1 :(得分:0)

我没有从你的描述中得到任何东西,但是如果你希望你的服务器回复你将要进入命令提示符,你应该改变你的“in.readLine”

 while (x) {
        reply= stdIn.readLine();
        if(reply!=null)
        {
            x=false;
        }
    }

另外如果你想要打印ip地址和端口,你应该打印它,lol

System.out.println("InetAdress " + echoSocket.getInetAddress() + ":" + echoSocket.getPort());

答案 2 :(得分:0)

检查这个

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);
    }
}

}

答案 3 :(得分:-1)

试试这个

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

应打印“在端口8080上连接到serverName