java - 使用 java 套接字的客户端-服务器应用程序中的 "Stream closed"IOException

标签 java sockets stream

由于“流关闭”异常,我的客户端中断了。 服务器正确等待连接,但客户端由于“流关闭”异常而未发送任何数据。 等待一段时间后服务器回显“意外错误”。

感谢您的帮助!

我的代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private static final int PORT = 50000;
    static boolean flaga = true;

    private static ServerSocket serverSocket;
    private static Socket clientSocket;

    public static void main(String[] args) throws IOException {
        serverSocket = null;
        try {
            serverSocket = new ServerSocket(PORT);
        } catch (IOException e) {
            System.err.println("Could not listen on port: " + PORT);
            System.exit(1);
        }

        System.out.print("Wating for connection...");

        Thread t = new Thread(new Runnable() {
            public void run() {
                try {
                    while (flaga) {
                        System.out.print(".");
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException ie) {
                    //
                }

                System.out.println("\nClient connected on port " + PORT);
            }
        });
        t.start();

        clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
            flaga = false;
        } catch (IOException e) {
            System.err.println("Accept failed.");
            t.interrupt();
            System.exit(1);
        }

        final PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
                true);
        final BufferedReader in = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));

        t = new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(5000);

                    while (true) {
                        out.println("Ping");
                        System.out.println(System.currentTimeMillis()
                                + " Ping sent");

                        String input = in.readLine();

                        if (input.equals("Pong")) {
                            System.out.println(System.currentTimeMillis()
                                    + " Pong received");
                        } else {
                            System.out.println(System.currentTimeMillis()
                                    + " Wrong answer");
                        }

                        Thread.sleep(5000);
                    }
                } catch (Exception e) {
                    System.err.println(System.currentTimeMillis()
                            + " Unexpected Error");
                }
            }
        });
        t.start();
    }
}

和客户端代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {
    private static final int PORT = 50000;
    private static final String HOST = "127.0.0.1";

    public static void main(String[] args) throws IOException {
        Socket socket = null;

        try {
            socket = new Socket(HOST, PORT);
        } catch (Exception e) {
            System.err.println("Could not connect to " + HOST + ":" + PORT);
            System.exit(1);
        }

        final PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        final BufferedReader in = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));

        Thread t = new Thread(new Runnable() {
            public void run() {
                long start = System.currentTimeMillis();

                try {
                    while (true) {
                        try {
                            String input = in.readLine();

                            if (input != null) {
                                System.out.println(System.currentTimeMillis()
                                        + " Server: " + input);
                            }

                            if (input.equals("Ping")) {
                                if (System.currentTimeMillis() - start > 30000) {
                                    out.println("Pon g");
                                    System.out.println(System
                                            .currentTimeMillis()
                                            + " Client: Pon g");
                                    break;
                                }

                                out.println("Pong");
                                System.out.println(System.currentTimeMillis()
                                        + " Client: Pong");
                            } else {
                                System.out.println(start);
                                out.println("got");
                            }
                        } catch (IOException ioe) {
                            System.err.println(System.currentTimeMillis() + " "
                                    + ioe.getMessage());
                            ioe.getStackTrace();
                            System.exit(0);
                        }
                    }
                } catch (Exception e) {
                    System.err.println(System.currentTimeMillis()
                            + " Unexpected Error");
                }
            }
        });
        t.start();

        out.close();
        in.close();
        socket.close();
    }
}

最佳答案

在您的客户端中,您启动线程但直接关闭流和套接字:

t.start();
out.close();
in.close();
socket.close();

作为测试,您可以将流和套接字调用移至最后一个 catch block 。

 ...
        } catch (Exception e) {
                System.err.println(System.currentTimeMillis()
                        + " Unexpected Error");     
                out.close();
                in.close();
                socket.close();
            }
        }
    });
    t.start();
}

关于java - 使用 java 套接字的客户端-服务器应用程序中的 "Stream closed"IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20050554/

相关文章:

java - 查找缺失数字的时间复杂度 O(n)?

java - 多个线程将对象引用传递给静态辅助方法

c++ - 从文件中读取和显示字符串

java - 在 Java 中强制目标打印机

java - 查找数据库结构中子节点的数量

c# - C# 中的 Tcp 套接字服务器

Java套接字绑定(bind)抛出异常但工作正常

Java - 使用 TCP 套接字接收通知

delphi - 为什么在将资源复制到 Web 响应中时会发生访问冲突?

c# - System.IO.Stream 中的模式搜索