0

I want to send some Object with sockets, but this doesn't work always. Sometimes it works, sometimes not. And then I get a StreamCorruptedException. Why do I get it? Here my client:

DataOutputStream os = new DataOutputStream(socket.getOutputStream());
os.writeUTF(output);
os.flush();
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
Post[] returnArray;
while ((returnArray = (Post[]) in.readObject()) != null) {
       return returnArray;
 }

I get the error here: ObjectInputStream in = new ObjectInputStream(socket.getInputStream());

And at the server I have this:

public void returnObjectPosts(Post[] sendPosts){
    try{
        ObjectOutputStream os = new ObjectOutputStream(client.getOutputStream());
        os.writeObject(sendPosts);
        os.flush();
        os.close();
    }catch(UnknownHostException e){
        e.printStackTrace();
    }catch(IOException e){
        e.printStackTrace();
    }
}
user6586661
  • 432
  • 1
  • 11
  • 24
  • NB `readObject()` does not return null at end of stream. It throws `EOFException`. – user207421 Oct 09 '16 at 18:47
  • @EJP Sorry, I don't really know what you mean. – user6586661 Oct 09 '16 at 18:57
  • Your loop reads until null is retuned. Null can be returned any time you send a null. Null is not a valid test for end of an object stream. You should be catching the `EOFException` that is thrown. – user207421 Oct 09 '16 at 23:22
  • @EJP thanks for your help, I use now EOFException, but my problem isn't solved yet. Also the other post doesn't help me.. – user6586661 Oct 11 '16 at 14:58

0 Answers0