简体   繁体   English

NetworkStream.Write 阻塞到什么时候?

[英]Until when does NetworkStream.Write block?

I can think of these possible answers:我能想到这些可能的答案:

  • Until the data is written to some internal buffer in the IP stack.直到数据写入 IP 堆栈中的某个内部缓冲区。
  • Until the data is sent over the wire.直到数据通过网络发送。
  • Until a confirmation of reception is received from the other machine.直到收到来自另一台机器的接收确认。

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.当发送的字节数约为 SenderSendBuffer + ReceiverReceiveBuffer 时,套接字将被阻塞。

NetworkStream does not buffer data. NetworkStream不缓冲数据。 Ultimately, a call to NetworkStream.Write translates into socket send function call.最终,对NetworkStream.Write的调用将转换为套接字发送function 调用。 MSDN article for this function says:此 function 的 MSDN 文章说:

The successful completion of a send function does not indicate that the data was successfully delivered and received to the recipient.发送 function 成功完成并不表示数据已成功交付并接收到接收方。 This function only indicates the data was successfully sent.这个function只表示数据发送成功。

Does this answer your question?这回答了你的问题了吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM