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

时间:2013-11-18 14:45:23

标签: java sockets stream

我的客户端因为“Stream closed”异常而中断。 服务器正确等待连接,但由于“流关闭”异常,客户端不发送任何数据。 等待时间后的服务器回应“意外错误”。

感谢您的帮助!

我的代码:

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

1 个答案:

答案 0 :(得分:1)

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

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

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

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