Java — How to read an unknown number of bytes from an inputStream (socket/socketServer)?

后端 未结 11 1030
梦如初夏
梦如初夏 2020-12-15 07:42

Looking to read in some bytes over a socket using an inputStream. The bytes sent by the server may be of variable quantity, and the client doesn\'t know in advance the lengt

11条回答
  •  清歌不尽
    2020-12-15 08:34

    Either:

    1. Have the sender close the socket after transferring the bytes. Then at the receiver just keep reading until EOS.

    2. Have the sender prefix a length word as per Chris's suggestion, then read that many bytes.

    3. Use a self-describing protocol such as XML, Serialization, ...

提交回复
热议问题