简体   繁体   English

Linux TCP / IP套接字流的非阻塞发送..TCP接收缓冲区发生了什么?

[英]Linux TCP/IP Non-blocking send for socket stream..what happens to the TCP recv buffer?

This pertains to Linux kernel 2.6 TCP sockets. 这与Linux内核2.6 TCP套接字有关。

I am sending a large amount of data, say 300 MB, with a non-blocking send to another client who receives 8 MB at a time. 我正在发送大量数据,例如300 MB,并且无阻塞地发送给另一个每次接收8 MB的客户端。

After one 8 MB receive, the "receiver" stops receiving because it wants to perform other tasks, such as error handling. 接收到一个8 MB内存后,“接收器”将停止接收,因为它想执行其他任务,例如错误处理。 The sender would get a EWOULDBLOCK, but since it's asynchronous communication, the send would try and fill up the TCP recv buffer on the other end. 发送方将获得一个EWOULDBLOCK,但是由于它是异步通信,因此发送方将尝试填充另一端的TCP recv缓冲区。

My question is: would there still be data in the TCP recv buffer even though the "sender" got a EWOULDBLOCK and the "receiver" stops receiving? 我的问题是:即使“发送者”得到了一个EWOULDBLOCK并且“接收者”停止了接收,TCP recv缓冲区中是否仍然会有数据? The same socket is used for error handling, so would the "receiver" have to then clear the TCP recv buffer before trying to reuse the existing socket? 使用同一套接字进行错误处理,那么“接收器”是否必须在尝试重用现有套接字之前清除TCP recv缓冲区?

Yes. 是。 It is quite possible (and in fact likely) that when you get EWOULDBLOCK , some data you have already sent has not yet been read by the recieving application. 当您收到EWOULDBLOCK ,很有可能(实际上是可能的)接收的应用程序尚未读取您已经发送的某些数据。 This buffered data will be available to the next read on the socket. 缓冲的数据将可供套接字上的下一次read使用。

This means that if your reciever then sends a "Ooops, don't send any more" message back to the sender, the sender can't act on that message and "un-send" the data. 这意味着,如果您的接收方随后向发件人发送“糟糕,不要再发送”消息,发件人将无法对该消息采取行动并“取消发送”数据。 Once it's been passed to write() / send() , it's on its way and can't be recalled. 一旦将其传递给write() / send() ,它就在路上,无法调用。

Your receiver will have to handle this eventuality by reading out the data it's no longer interested in and discarding it, which will mean you'll need some kind of transaction delimiters in your data stream. 接收者必须通过读出不再感兴趣的数据并丢弃它来处理这种可能,这意味着您将在数据流中需要某种事务定界符。

My question is: would there still be data in the TCP recv buffer even though the "sender" got a EWOULDBLOCK and the "receiver" stops receiving? 我的问题是:即使“发送者”得到了一个EWOULDBLOCK并且“接收者”停止了接收,TCP recv缓冲区中是否仍然会有数据?

There is data in the TCP receive buffer because the sender got EWOULDBLOCK. TCP接收缓冲区中有数据, 因为发送者获得了EWOULDBLOCK。 That's the only condition it can happen under. 这是它可能发生的唯一条件。

Your question doesn't make sense. 您的问题没有道理。

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

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