Is an OutputStream in Java blocking? (Sockets)

后端 未结 3 1103
清歌不尽
清歌不尽 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

    Of course, when you write to a socket, this write is buffered. Socket object have a setSendBufferSize() method for setting this buffer size. If your write can be cached in this buffer, then of course, you may iterate immediately on the following socket. Otherwise this buffer need to be flushed to the client immediately. So, during flushing you are going to be blocked. If you want to avoid being blocked while flushing the buffer, you have to use a SocketChannel in non blocking I/O. Anyway, the best option for writing to many socket concurrently, is to manage each socket with a different thread, so that all writes may be executed at the same time.

提交回复
热议问题