EOFException in objectinputstream readobject

萝らか妹 提交于 2020-04-07 09:35:13

问题


i want to make a server client application. i set everything up and i get this error. i want the app to wait till i get more data.

java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)

on the line:

listener.Received(inputstream.readObject(), id.ID);

Server code:

isrunning = true;
    Thread input = new Thread(new Runnable(){
        @Override
        public void run() {
            try{
                ServerSocket Server = new ServerSocket(LocalPort);
                while(isrunning){
                    Socket socket = Server.accept();
                    ObjectOutputStream outputstream = new ObjectOutputStream(socket.getOutputStream());
                    ObjectInputStream inputstream = new ObjectInputStream(socket.getInputStream());
                    Object obj = inputstream.readObject();
                    if(obj instanceof ID){
                        ID id = (ID) obj;
                        if(connctedChecker(id.ID)){
                            ID myid = new ID(ID, LocalIP, LocalPort);
                            outputstream.writeObject(myid);
                            connect(id.IP, id.Port);
                            listener.Connected(id.ID);
                            do{
                                listener.Received(inputstream.readObject(), id.ID);
                            }while(socket.isConnected());
                            listener.Disconnected(id.ID);
                            closeConnection(id.ID);
                        }
                    }
                    inputstream.close();
                    outputstream.close();
                    socket.close();
                }
                Server.close();
            }catch(Exception e){
                e.printStackTrace();
            }

        }
    });
    input.start();

Client code:

output = new Thread(new Runnable(){
        @Override
        public void run(){
            try {
                socket = new Socket(RemoteIP, RemotePort);
                inputstream = new ObjectInputStream(socket.getInputStream());
                outputstream = new ObjectOutputStream(socket.getOutputStream());
                ID id = new ID(ID, LocalIP, LocalPort);
                outputstream.writeObject(id);
                outputstream.flush();
                Object obj = inputstream.readObject();
                if(obj instanceof ID){
                    ID inid = (ID) obj;
                    RemoteID = inid.ID;
                }
                while(socket.isConnected()){
                    Object object = queue.take();
                    outputstream.writeObject(object);
                    outputstream.flush();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

the listener:

public class Listener {

public void Connected(UUID ID){

}

public void Received(Object object, UUID ID){

}

public void Disconnected(UUID ID){

}

}

回答1:


do{
    listener.Received(inputstream.readObject(), id.ID);
} while(socket.isConnected());

Unless your socket/inputstream has an infinite number of objects to send, it is going to run out of objects sooner or later. That's what has happened. It says so right in the javadoc for EOFException.

Signals that an end of file or end of stream has been reached unexpectedly during input.

This exception is mainly used by data input streams to signal end of stream. Note that many other input operations return a special value on end of stream rather than throwing an exception.




回答2:


Are all your Objects used in WorkerMessageToMaster serializable, as well as WorkerMessageToMaster itself? Every Object you want to send with ObjectStreams have to implement serializable, and all children of it have to have it implemented. String, as well as primitive datatypes (int, float etc) are serializable without you needing to do anything. But if you use your own classes, they have to implement serializable.



来源:https://stackoverflow.com/questions/31885999/eofexception-in-objectinputstream-readobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!