Until when does NetworkStream.Write block?

◇◆丶佛笑我妖孽 提交于 2019-12-05 02:12:49

问题


I can think of these possible answers:

  • Until the data is written to some internal buffer in the IP stack.
  • Until the data is sent over the wire.
  • Until a confirmation of reception is received from the other machine.

回答1:


Until data is written to the send buffer on the sender side.
So if buffer is full, it will block.

The send buffer can be full if it didn't transmit data yet, because of network issues or because receive buffer is full on the receiver side.

There is an experiment you can conduct: make a sender and receiver, set sender's socket send buffer to something small and receiver's receive buffer to something small to.

Start sending, accept connection on the receiver side, but don't receive. The socket will be blocked when the sent bytes number is about SenderSendBuffer + ReceiverReceiveBuffer.




回答2:


NetworkStream does not buffer data. Ultimately, a call to NetworkStream.Write translates into socket send function call. MSDN article for this function says:

The successful completion of a send function does not indicate that the data was successfully delivered and received to the recipient. This function only indicates the data was successfully sent.

Does this answer your question?



来源:https://stackoverflow.com/questions/6262626/until-when-does-networkstream-write-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!