Is an OutputStream in Java blocking? (Sockets)

后端 未结 3 964
清歌不尽
清歌不尽 2021-02-19 03:35

I am currently writing naive network code for a project and a mate hinted me at the possibility that when I send a package of information from the server to all clients in an it

3条回答
  •  终归单人心
    2021-02-19 03:58

    An OutputStream is blocking. It probably has some buffering, but that doesn't help you much if the server is never consuming bytes (any fixed buffer will eventually fill up). So your friend is right, you need to write in a separate thread, or use something more advanced, like nio.

    On the reading side, you can use available() to avoid blocking. No matching call exists on the write side. I wish there was.

提交回复
热议问题