简体   繁体   English

为什么我的ObjectInputStream抛出EOFException?

[英]Why is my ObjectInputStream throwing an EOFException?

I am writing a chat room code and each message is sent back and forth between client and server constantly. 我正在编写一个聊天室代码,并且每条消息都会在客户端和服务器之间不断地来回发送。 I have made all the messages into serialized objects and am using them to pass the information back and forth. 我已将所有消息放入序列化的对象中,并正在使用它们来回传递信息。 Unfortunately this has led to an EOFException being thrown at the very first time the object is being read. 不幸的是,这导致在第一次读取对象时引发EOFException。 The error code is as follows: 错误代码如下:

Exception in thread "main" java.io.EOFException
    at java.io.DataInputStream.readInt(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.readInt(Unknown Source)
    at java.io.ObjectInputStream.readInt(Unknown Source)
    at Serverside.ChatObject.readObject(ChatObject.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
    at java.io.ObjectInputStream.readSerialData(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at Clientside.Client.run(Client.java:161)
    at Clientside.Client.main(Client.java:233)

I am not sure if this is caused by the ObjectOutputStream never being closed, but if so, how do I work around this? 我不确定这是否是由于ObjectOutputStream从未关闭导致的,但是如果是这样,我该如何解决? Would I have to reopen the stream every single time an object is going to be written and can the input stream be left in the loop to wait for data? 我是否必须在每次要写入对象时重新打开流,并且输入流可以留在循环中以等待数据吗?

This is my custom object read/write code: 这是我的自定义对象读/写代码:

    private void writeObject(java.io.ObjectOutputStream stream) throws IOException{
    stream.writeObject(type);
    stream.writeObject(chatroom);
    stream.writeObject(target);
    stream.writeObject(sender);
    stream.writeObject(message);
    stream.writeObject(users);
    stream.close();
}

private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException{
    type = stream.readInt();
    chatroom = stream.readInt();
    target = (String) stream.readObject();
    sender = (String) stream.readObject();
    message = (String) stream.readObject();
    users = (ArrayList<String>) stream.readObject();
    stream.close();
}

And this is the first bit of code once the thread has been started on the server side. 一旦线程在服务器端启动,这就是代码的第一位。 The issue could not have happened beyond this because the other methods will not have had the ability to be called yet. 除此之外,不可能发生此问题,因为其他方法尚未具有被调用的能力。

    public void run(){
        try{
            out = new ObjectOutputStream(socket.getOutputStream());
            in = new ObjectInputStream(socket.getInputStream());

            while(true){
                out.writeObject(new ChatObject(0));
                out.flush();
                info = (ChatObject) in.readObject();
                name = info.getSender();
                if(name == null){
                    return;
                }
                synchronized (names){
                    if(!names.contains(name)){
                        names.add(name);
                        break;
                    }
                }
            }

If anyone feels it is neccessary, I can include more code as needed (from the object or from the client/writing side). 如果有人觉得有必要,我可以根据需要添加更多代码(从对象或从客户端/编写端)。

A quick look at the javadoc entry for DataInputStream#readInt ( link ) taught me that, this method throws an EOFException (which stands for End Of File Exception ) when the stream you're trying to read and int from ends within less than 4 bytes (a Java int being made of four bytes ). 快速浏览一下DataInputStream#readIntlink )的javadoc条目,告诉我,当您尝试读取的流的长度小于4个字节且从int 结束读取int时,此方法将引发EOFException (代表End Of File Exception )。 (一个Java int四个字节组成 )。

You may need to use some interface that blocks until the stream contains enough data :) 您可能需要使用一些阻塞的接口,直到流包含足够的数据为止:)

If you read type with type = stream.readInt(); 如果使用type = stream.readInt();读取类型 it should be written with stream.writeInt(type); 应该用stream.writeInt(type);来写

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

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