简单服务器客户端程序中的EOF-和SocketException

时间:2019-03-16 14:48:25

标签: java networking

我今天写了一个简单的服务器客户端程序,但是我有一个问题,不知道为什么。 这是我的3个重要类(所有方法均正确调用):

    public class Server {
    private ServerSocket server;
    private ArrayList<Client> clients = new ArrayList<Client>();
    public Server(int port) {
        try {
            server = new ServerSocket(port);
            server.setSoTimeout(900000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        acceptClients();
        Main.frame.log("Server started on port "+port);
    }
    public void acceptClients() {
        Thread t = new Thread() {
            @Override
            public void run() {
                System.out.println("Accepting");
                while(true) {
                    try {
                        Socket client = server.accept();
                        System.out.println("Accepted: "+client.getLocalSocketAddress());
                        clients.add(new Client(client));
                    } catch (IOException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            };
        };
        t.start();
    }

}

    public class Client {
    private static Socket client;
    private static DataOutputStream out;
    private static DataInputStream in;
    public static void main(String[] args) throws UnknownHostException, IOException {
        client = new Socket("localhost", 1567);
        client.setSoTimeout(900000);
        in = new DataInputStream(client.getInputStream());
        out = new DataOutputStream(client.getOutputStream());
        receive();
        send("Hi I am a client!");
    }

    public static void receive() {
        Thread t = new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        String data = in.readUTF();
                        System.out.println("Received: "+data);
                        send("Test");
                    } catch (IOException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            };
        };
        t.start();
    }

    public static void send(String data) {
        try {
            System.out.println("Sending");
            out.writeUTF(data);
            System.out.println("Sent: "+data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

我的服务器的客户端处理程序:

    public class Client {
    Socket socket;
    DataOutputStream out;
    DataInputStream in;
    public Client(Socket client) {
        socket = client;
        try {
            socket.setSoTimeout(900000);
            in = new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());
            receive();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void receive() {
        Thread t = new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        process(in.readUTF());
                        in.close();
                        sleep(100);
                    } catch (IOException | InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        };
        t.start();
    }
    public void send(String data) {
        try {
            out.writeUTF(data);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void process(String data) {
        Main.frame.log("Received--> "+data);
        send("I received your message!");
    }

}

当我执行服务器和客户端时,一切正常,直到客户端发送第二条消息为止。然后我从服务器收到此错误:

java.net.SocketException: socket closed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.io.DataInputStream.readUnsignedShort(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at de.julian.factoryserver.net.Client$1.run(Client.java:33)

来自我的客户的错误:

java.io.EOFException
    at java.io.DataInputStream.readUnsignedShort(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at java.io.DataInputStream.readUTF(Unknown Source)
    at ftc.Client$1.run(Client.java:28)

我希望有人能帮助我!

1 个答案:

答案 0 :(得分:1)

在类Server中,在方法send中,您似乎已经关闭了输出流,不要关闭它,只需刷新它即可。

 public void send(String data) {
        try {
            out.writeUTF(data);
            out.close(); // remove this and replace it with out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

也在recieve中,您似乎过早关闭了输入流

private void receive() {
        Thread t = new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        process(in.readUTF());
                        in.close(); // remove this
                        sleep(100);
                    } catch (IOException | InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        };
        t.start();
    }

应用上述修复程序,应该可以修复您的异常。