当我将数据写入阻塞套接字时会发生什么,比另一端读取速度快? [英] what happens when I write data to a blocking socket, faster than the other side reads?

查看:28
本文介绍了当我将数据写入阻塞套接字时会发生什么,比另一端读取速度快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我将数据写入速度非常快[我有内存中的所有数据] 到阻塞套接字.进一步假设另一方读取数据的速度非常慢[例如每次读取之间休眠 1 秒].

suppose I write data really fast [I have all the data in memory] to a blocking socket. further suppose the other side will read data very slow [like sleep 1 second between each read].

在这种情况下,写入方面的预期行为是什么?写操作会阻塞直到对方读取足够的数据,还是会返回连接重置等错误?

what is the expected behavior on the writing side in this case? would the write operation block until the other side reads enough data, or will the write return an error like connection reset?

推荐答案

对于阻塞套接字,send() 调用将阻塞,直到所有数据都复制到网络堆栈的缓冲区中为止联系.它不必由另一方接收.此缓冲区的大小取决于实现.

For a blocking socket, the send() call will block until all the data has been copied into the networking stack's buffer for that connection. It does not have to be received by the other side. The size of this buffer is implementation dependent.

当远程端确认时,数据从缓冲区中清除.这是操作系统的事情,不依赖于实际读取数据的远程应用程序.此缓冲区的大小也取决于实现.

Data is cleared from the buffer when the remote side acknowledges it. This is an OS thing and is not dependent upon the remote application actually reading the data. The size of this buffer is also implementation dependent.

当远程缓冲区已满时,它会告诉您的本地堆栈停止发送.当数据从远程缓冲区中清除(被远程应用程序读取)时,远程系统会通知本地系统发送更多数据.

When the remote buffer is full, it tells your local stack to stop sending. When data is cleared from the remote buffer (by being read by the remote application) then the remote system will inform the local system to send more data.

在这两种情况下,小型系统(如嵌入式系统)可能有几 KB 或更小的缓冲区,而现代服务器可能有几 MB 或更大的缓冲区.

In both cases, small systems (like embedded systems) may have buffers of a few KB or smaller and modern servers may have buffers of a few MB or larger.

一旦本地缓冲区中有可用空间,来自 send() 调用的更多数据将被复制.复制所有这些数据后,您的调用将返回.

Once space is available in the local buffer, more data from your send() call will be copied. Once all of that data has been copied, your call will return.

除非连接确实被重置,否则您不会收到连接重置"错误(来自操作系统——库可能会做任何事情).

You won't get a "connection reset" error (from the OS -- libraries may do anything) unless the connection actually does get reset.

所以...在您发送与本地和本地应用程序一样多的数据之前,远程应用程序读取数据的速度实际上并不重要.远程缓冲区大小相结合.之后,您将只能像远程端 recv() 一样快地send().

So... It really doesn't matter how quickly the remote application is reading data until you've sent as much data as both local & remote buffer sizes combined. After that, you'll only be able to send() as quickly as the remote side will recv().

这篇关于当我将数据写入阻塞套接字时会发生什么,比另一端读取速度快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆