Java 程序在套接字编程中抛出 EOFException,即使在输出流被刷新后也是如此



这是错误的完整堆栈跟踪

java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2323)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2792)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:800)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
    at mypackage.JChatComm.receiveMessage(JChatComm.java:83)
    at mypackage.JThread.run(JThread.java:19)
    at java.lang.Thread.run(Thread.java:701)

这是我的接收消息代码:此函数创建对象 JPacket 的 ObjectInputStream 并打印该对象。但是在一次迭代之后,它抛出了EOFException并打印了4,11,8和stackTrace。

public boolean receiveMessage() throws IOException, ClassNotFoundException{
        try{
            System.out.println("4");
        InputStream inFromServer = client.getInputStream();
        System.out.println("11");
        ObjectInputStream in = new ObjectInputStream(inFromServer);
        //System.out.println("hellow owrol");
        //System.out.println("5");
        final JPacket jp;
         jp = (JPacket) in.readObject();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                System.out.println("6");
                if (type.equals("s")){
                    tab.ta.append("Client: "+jp.msg+"n");
                }
                else {
                    cp.ta.append("Server: "+jp.msg+"n");
                }
            }
        });
        System.out.println("other: "+jp.msg+ "     Sent on:" + jp.a1);
        //System.out.println("msg got");
        return true;
    }
    catch (Exception ea){
        System.out.println("8");
        ea.printStackTrace();
        return false;
    }
    }

这是我的发送消息程序该程序是发送消息的主程序。它始终工作正常并正确刷新输出流。但仍然收到消息抛出错误。

public boolean sendMessage() throws IOException{
        try{
            System.out.println("3");
        final String msg;
        if (type.equals("c")){
            msg = cp.tf.getText();
        }
        else msg = tab.tf.getText();
        System.out.println(msg);
        JPacket j1 = new JPacket(msg);
        OutputStream outToServer = client.getOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(outToServer);
        out.writeObject(j1);
        System.out.println("13");
        out.flush();
        System.out.println("12");
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                if (type.equals("s")){
                    tab.ta.append("You: "+msg+"n");
                    tab.tf.setText("");
                }
                else {
                    System.out.println("Client");
                    cp.ta.append("You: "+msg+"n");
                    cp.tf.setText("");
                }
            }
        });
        if(msg.equals("End Chat")) {
            endChat();
            return false;
        }
        else
            return true;
    }
    catch (Exception ea){
        ea.printStackTrace();
        System.out.println("7");
        return false;
    }
}

我每次都刷新对象输出流,但仍然抛出相同的错误。

不要为每条消息创建新流。在套接字的两端使用单个 ObjectInoutStream 和 ObjectOutputStream 作为套接字的生命周期。这些流具有流标头,重新创建它们只是完全不同步的机会。

最新更新