Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
客户端/服务器应用程序连接重置Java_Java_Networking_Client - Fatal编程技术网

客户端/服务器应用程序连接重置Java

客户端/服务器应用程序连接重置Java,java,networking,client,Java,Networking,Client,当我在运行服务器后运行客户端时,服务器会因错误连接重置而崩溃。。。这是我的密码: 启动客户端套接字并连接到服务器。等待输入。 客户: 启动服务器套接字并向客户端发送“2”并启动对话 服务器: public ServerTest() { try { serverSocket = new ServerSocket(25565); clientSocket = serverSocket.accept(); } catch (IOExc

当我在运行服务器后运行客户端时,服务器会因错误连接重置而崩溃。。。这是我的密码:

启动客户端套接字并连接到服务器。等待输入。 客户:

启动服务器套接字并向客户端发送“2”并启动对话

服务器:

    public ServerTest() {
    try {
        serverSocket = new ServerSocket(25565);
        clientSocket = serverSocket.accept();

    } 
    catch (IOException e) {
        System.out.println("Could not listen on port: 4444");
        System.exit(-1);
    }
    start();
}

public void start() {

    try {
        PrintWriter out;
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String inputLine, outputLine;
        // initiate conversation with client
        out.println("2");
            while ((inputLine = in.readLine()) != null) {   
                System.out.println(inputLine);
                out.println("2");
            }
        System.out.println("Stopping");
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public static void main(String[] args) {
    new ServerTest();
}

当我运行服务器时,一切正常,但当我运行客户端之后,服务器崩溃,出现连接重置错误。

ClientTest()不调用start()方法。您的客户端在建立连接后立即退出。

Alex的回答是正确的


这个程序也进入无限循环。您需要在客户端和服务器的while循环中添加一个退出条件。

您可以添加堆栈跟踪吗?但它会立即崩溃,说连接重置我认为这与无限循环无关
    public ServerTest() {
    try {
        serverSocket = new ServerSocket(25565);
        clientSocket = serverSocket.accept();

    } 
    catch (IOException e) {
        System.out.println("Could not listen on port: 4444");
        System.exit(-1);
    }
    start();
}

public void start() {

    try {
        PrintWriter out;
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String inputLine, outputLine;
        // initiate conversation with client
        out.println("2");
            while ((inputLine = in.readLine()) != null) {   
                System.out.println(inputLine);
                out.println("2");
            }
        System.out.println("Stopping");
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public static void main(String[] args) {
    new ServerTest();
}