即使在刷新outputstream之后,Java程序也会在套接字编程中抛出EOFException

时间:2014-04-08 03:54:47

标签: java sockets network-programming objectinputstream objectoutputstream

这是错误的完整堆栈跟踪

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)

这是我的receiveMessage代码: 此函数创建对象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;
    }

    }

这是我的sendMessage程序 该程序是发送消息的主程序。它始终正常工作并正确刷新输出流。但仍然是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;
    }
}

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

1 个答案:

答案 0 :(得分:0)

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