简体   繁体   English

即使刷新输出流,Java程序也会在套接字编程中引发EOFException

[英]Java program throwing EOFException in socket programming even after outputstream is flushed

Here is the complete stacktrace of the error 这是错误的完整堆栈跟踪

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)

Here is my receiveMessage code: This function creates a ObjectInputStream of a object JPacket and print that object. 这是我的receiveMessage代码:此函数创建对象JPacket的ObjectInputStream并打印该对象。 But after one iteration, it is throwing EOFException and printing 4,11,8 and stackTrace. 但是经过一轮迭代后,它抛出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;
    }

    }

and here is my sendMessage Program This programs is the main program to send the message. 这是我的sendMessage程序,该程序是发送消息的主程序。 It is working fine always and properly flushing the outputstream. 它始终可以正常工作并正确冲洗输出流。 But still receiveMessage is throwing error. 但是receiveMessage仍然抛出错误。

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

I am flushing the object output stream everytime but still it throwing the same error. 我每次都刷新对象输出流,但仍然抛出相同的错误。

Don't create new streams per message. 不要为每条消息创建新的流。 Use a single ObjectInoutStream and ObjectOutputStream for the life of the socket, at both ends. 在套接字的两端使用单个ObjectInoutStream和ObjectOutputStream。 These streams have stream headers, and recreating them is just an opportunity to get completely out of sync. 这些流具有流头,重新创建它们只是完全不同步的机会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM