简体   繁体   English

什么条件导致NetworkStream.Write阻塞?

[英]What conditions cause NetworkStream.Write to block?

Will NetworkStream.Write block only until it places the data to be sent into the TCP send buffer, or will it block until the data is actually ACK'd by the receiving host? NetworkStream.Write只会阻塞它将数据发送到TCP发送缓冲区,还是会阻塞,直到接收主机实际确认数据为止?

Note: The socket is configured for blocking I/O. 注意:套接字配置为阻止I / O.

Edit: Whoops, there's no such thing as TcpClient.Write of course! 编辑:哎呀,当然没有TcpClient.Write这样的东西! We all understood we were talking about TcpClient.GetStream().Write , which is actually NetworkStream.Write ! 我们都明白我们在谈论TcpClient.GetStream().Write ,实际上是NetworkStream.Write

Unless .net is using something other than winsock, then according to the winsock reference: 除非.net使用的不是winsock,否则根据winsock参考:

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. 此功能仅表示数据已成功发送。

If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode. 如果传输系统中没有可用的缓冲区空间来保存要传输的数据,则发送将阻止,除非套接字已置于非阻塞模式。 On nonblocking stream oriented sockets, the number of bytes written can be between 1 and the requested length, depending on buffer availability on both the client and server computers. 在非阻塞流定向套接字上,写入的字节数可以在1和请求的长度之间,具体取决于客户端和服务器计算机上的缓冲区可用性。

Assuming that write is calling send underneath, then a strict interpretation of the winsock documentation would indicate that there is no gurantee that the data made it to the other end of the pipe when it returns. 假设write在下面调用send,那么对winsock文档的严格解释将表明没有保证数据在返回时将数据传递到管道的另一端。

Here is the link to the winsock docs I am quoting from: http://msdn.microsoft.com/en-us/library/windows/desktop/ms741416(v=VS.85).aspx 以下是我引用的winsock文档的链接: http//msdn.microsoft.com/en-us/library/windows/desktop/ms741416( v = VS.85) .aspx

I disagree with both answers [that state it blocks]. 我不同意这两个答案[说明它阻止]。 Writing to TCP/IP socket does not block unless the underlying buffer is already full of unacknowledge data. 写入TCP / IP套接字不会阻止,除非底层缓冲区已经充满了未确认数据。 Generally, it doesn't block but just gets handed off to the TCP implementation. 通常,它不会阻塞,而只是传递给TCP实现。 But of course now I have to go track down some references to back this up :) 但是当然现在我必须追踪一些参考来支持这个:)

From SO 来自SO

TcpClient.Write will block until the packet buffer has been flushed to the network and the appropriate ACK(s) have been received. TcpClient.Write将阻塞,直到数据包缓冲区已刷新到网络并且已收到相应的ACK。 You'll notice that a dropped connection will usually end up throwing an exception on the Write operation, since it waits for the ACK but doesn't get one within the defined timeout period. 您会注意到,断开的连接通常最终会在Write操作上抛出异常,因为它等待ACK但在定义的超时时间内没有得到ACK。

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

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