What conditions cause NetworkStream.Write to block?

后端 未结 3 1669
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 00:13

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 rece

相关标签:
3条回答
  • 2020-12-18 00:26

    TcpClient.Write will block until the packet buffer has been flushed to the network and the appropriate ACK(s) have been received. 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.

    0 讨论(0)
  • 2020-12-18 00:27

    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. Generally, it doesn't block but just gets handed off to the TCP implementation. But of course now I have to go track down some references to back this up :)

    From SO

    0 讨论(0)
  • 2020-12-18 00:32

    Unless .net is using something other than winsock, then according to the winsock reference:

    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.

    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.

    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

    0 讨论(0)
提交回复
热议问题