SocketException: Connection reset on server with ObjectInputStream

前端 未结 2 476
萌比男神i
萌比男神i 2021-01-24 19:15

I\'m trying to get my head around the ObjectInputStream/ObjectOutputStream, so I create a very simple server-client application where the client sends a HashMap object

2条回答
  •  别那么骄傲
    2021-01-24 20:16

    You're reading the object twice, but only printing the second time. So the client is probably shutdown by the time you try to read the second time, and so the socket has been closed on the remote end, hence exception.

    You're reading in an object on the != null check. You don't print it. Then you try to read it again and it bombs.

    Once you read from the stream, it's been read. It's a stream, not a buffer. You might want to buffer the read, and then you can inspect that buffer as much as you like.

    Simplest example...

    Object o = outputStream.readObject();
    if(o != null) {
     system.out.println(o);
    }
    

提交回复
热议问题