Non-blocking socket writes in Java versus blocking socket writes

后端 未结 2 745
暖寄归人
暖寄归人 2021-01-05 15:43

Why would someone prefer blocking writes over non-blocking writes? My understanding is that you would only want blocking write if you want to make sure the other side got th

2条回答
  •  既然无缘
    2021-01-05 16:25

    My understanding is that you would only want blocking write if you want to make sure the other side got the TCP packet once the write method returned

    Your understanding is incorrect. It doesn't ensure that.

    Blocking writes block until all the data has been transferred to the socket send buffer, from where it is transferred asynchronously to the network. If the reader is slow, his socket receive buffer will fill up, which will eventually cause your socket send buffer to fill up, which will cause a blocking write to block, blocking the whole thread. Non-blocking I/O gives you a way to detect and handle that situation.

提交回复
热议问题