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

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

问题描述

假设我确实非常快地将数据写入[我将所有数据存储在内存中]到阻塞套接字. 进一步假设另一端将读取数据非常慢(例如每次读取之间睡眠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.

因此...远程应用程序读取数据的速度并不重要,直到您发送的数据量与本地&远程缓冲区大小的总和.之后,您只能send()像远程端recv()一样快.

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天全站免登陆