C ++套接字编程最大大小的TCP / IP套接字缓冲区? [英] C++ socket programming Max size of TCP/IP socket Buffer?

查看:392
本文介绍了C ++套接字编程最大大小的TCP / IP套接字缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C ++ TCP / IP套接字。根据我的要求,我的客户端必须连接到一个服务器,并阅读它发送的消息(这是一个真正的新,不是它),但在我的应用程序,我不得不等待一段时间(通常是1 - 2小时)我实际开始阅读消息(通过recv()或read())之前,服务器仍然继续发送消息。

I am using C++ TCP/IP sockets. According to my requirements my client has to connect to a server and read the messages sent by it (that's something really new, isn't it) but... in my application I have to wait for some time (typically 1 - 2 hrs) before I actually start reading messages (through recv() or read()) and the server still keeps on sending messages.

我想知道是否有一个限制缓冲区的容量,保持这些消息,以防它们不被读取,并且其物理内存用于缓冲那些消息?发件人或收件人?

I want to know whether there is a limit on the capacity of the buffer which keeps those messages in case they are not read and whose physical memory is used to buffer those messages? Sender's or receiver's?

推荐答案

TCP数据在发送方和接收方缓冲。接收器套接字接收缓冲区的大小决定了在没有确认的情况下可以发送多少数据,发送方发送缓冲区的大小决定了在发送方阻塞或获取EAGAIN / EWOULDBLOCK之前可以发送多少数据,具体取决于阻塞/阻塞模式。你可以设置这些套接字缓冲区大到你喜欢的高达2 ^ 32-1字节,但是如果你设置客户端接收缓冲区高于2 ^ 16-1,你必须在连接套接字之前这样做,这样TCP窗口缩放可以在连接握手中协商,使高16位可以起作用。 [服务器接收缓冲区在这里不相关,但如果你设置它> = 64k你需要设置它在监听套接字,从哪里它将被接受的套接字继承,因此握手可以协商窗口缩放。]

TCP data is buffered at both sender and receiver. The size of the receiver's socket receive buffer determines how much data can be in flight without acknowledgement, and the size of the sender's send buffer determines how much data can be sent before the sender blocks or gets EAGAIN/EWOULDBLOCK, depending on blocking/non-blocking mode. You can set these socket buffers as large as you like up to 2^32-1 bytes, but if you set the client receive buffer higher than 2^16-1 you must do so before connecting the socket, so that TCP window scaling can be negotiated in the connect handshake, so that the upper 16 bits can come into play. [The server receive buffer isn't relevant here, but if you set it >= 64k you need to set it on the listening socket, from where it will be inherited by accepted sockets, again so the handshake can negotiate window scaling.]

但我完全同意马丁·詹姆斯的说法,这是一个愚蠢的要求。它在服务器上浪费线程,线程堆栈,套接字,大套接字发送缓冲区,FD和所有其他相关资源两个小时,并且可能影响其他线程,因此影响其他客户端。它还错误地给予服务器的印象是已经接收到两个小时的数据,当它实际上仅被发送到接收缓冲器时,这可能导致恢复情况下的未知并发症:例如,服务器可能不能重建到目前为止发送的数据。你最好不要连接,直到你准备好开始接收数据,否则读取和假脱机数据给自己在客户端以后处理。

However I agree entirely with Martin James that this is a silly requirement. It wastes a thread, a thread stack, a socket, a large socket send buffer, an FD, and all the other associated resources at the server for two hours, and possibly affects other threads and therefore other clients. It also falsely gives the server the impression that two hours' worth of data has been received, when it has really only been transmitted to the receive buffer, which may lead to unknown complications in recovery situations: for example, the server may be unable to reconstruct the data sent so far ahead. You would be better off not connecting until you are ready to start receiving the data, or else reading and spooling the data to yourself at the client for processing later.

这篇关于C ++套接字编程最大大小的TCP / IP套接字缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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