简体   繁体   中英

ObjectInputStream#readObject throws java.io.EOFException

I am getting a java.io.EOFException while trying to send data (It could be seen as a kind of POST although I am not using http) from a client back to the server.

My client source code:

Socket clientSocket = new Socket(serverIpAddr, IConstant.ServerPort);
OutputStream out = clientSocket.getOutputStream();
out.write(dataByteArray);
out.flush();
if(out!=null)
    out.close();
if(clientSocket!=null)
    clientSocket.close();

My server source code :

try{    
      ServerSocket serverSocket = new ServerSocket(IConstant.ServerPort);
      while(true)
      {
           Socket connectionSocket = serverSocket.accept();
           ObjectInputStream ois = new ObjectInputStream(connectionSocket.getInputStream());
           Object obj = ois.readObject();
           byte[] bytes = (byte[])obj;
           System.out.println("A notification has been received");
           //process bytes
           //...
       }
} catch(IOException ex){
     ex.printStackTrace();  
} catch (ClassNotFoundException e) {
     e.printStackTrace();
}

And this how I build my dataByteArray object (where PlayList is a POJO that implements java.io.Serializable) :

private byte[] serializePlayList(PlayList playList) throws IOException{
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] byteArray = null;
        ObjectOutput out = null;
        try {
          out = new ObjectOutputStream(bos);   
          out.writeObject(playList);
          out.flush();
          byteArray = bos.toByteArray();
        } finally {
          try {
            bos.close();
          } catch (IOException ex) {
          }
        }
        return byteArray;
    }

Whenever the server tries to perform ois.readObject() , a java.io.EOFException is thrown. Could anyone please explain why?

You should send playList in Object :

Client:

Socket clientSocket = new Socket(serverIpAddr, IConstant.ServerPort);
ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream();
out.writeObject(playlist);

Server:

ServerSocket serverSocket = new ServerSocket(IConstant.ServerPort);
Socket connectionSocket = serverSocket.accept();
ObjectInputStream ois = new ObjectInputStream(connectionSocket.getInputStream());
PlayList playList = (PlayList) ois.readObject();

you can using thread and get to start thread , the thread in spatial class

like this :

public class receive  extends Thread{
public   Socket socket = null;
ObjectInputStream din = null;
data_socket respon = null ;
data_socket dtsk = new data_socket();

public receive(Socket skt) {
        this.socket=skt;

     }
@Override
public void run(){
    try {

        din = new ObjectInputStream(this.socket.getInputStream());
        while(true){

                respon=(data_socket)din.readObject();

            switch(respon.action){
                case "login"             : this.check_login(); break;
                case "afeef"             : this.afeef();break;

                default                  : System.out.println("unknow action");
            }
        }
    } catch (IOException ex) {
        System.out.println("::::::::::::::::::ERROR 1::::::::::::::::"+ex.toString());
        System.out.println("You r Lost connect to server ");
       System.exit(0);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        System.out.println("::::::::::::::::::ERROR 2::::::::::::::::"+e.getException());
    }
}

I am getting a java.io.EOFException while trying to send data

No you aren't. You're getting it while trying to receive data.

Your sending code doesn't make sense. You should just serialize the PlayList object directly to an existing ObjectOutputStream , rather than all these shenanigans with ByteArrayOutputStreams and a new object stream per send.

And you should therefore also cast the result of readObject() directly to PlayList .

And the reading code should loop until EOFException has been caught, which means the peer closed the connection.

NB clientSocket and out cannot possibly be null at the points you are testing them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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