简体   繁体   English

C ++套接字编程最大TCP / IP套接字缓冲区大小?

[英]C++ socket programming Max size of TCP/IP socket Buffer?

I am using C++ TCP/IP sockets. 我正在使用C ++ TCP / IP套接字。 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. 根据我的要求,我的客户端必须连接到服务器并读取它发送的消息(这是真的很新,不是吗)但是...在我的应用程序中我必须等待一段时间(通常是1到2小时)在我真正开始阅读消息之前(通过recv()或read()),服务器仍然继续发送消息。

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 data is buffered at both sender and receiver. TCP数据在发送方和接收方都进行缓冲。 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. 接收器的套接字接收缓冲区的大小决定了在没有确认的情况下可以传输多少数据,并且发送方的发送缓冲区的大小决定了在发送方阻塞或获取EAGAIN / EWOULDBLOCK之前可以发送多少数据,具体取决于阻塞/非阻止阻止模式。 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. 您可以将这些套接字缓冲区设置为最大2 ^ 32-1字节,但如果将客户端接收缓冲区设置为高于2 ^ 16-1,则必须在连接套接字之前执行此操作,以便TCP窗口缩放可以在连接握手中进行协商,以便高16位可以起作用。 [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.] [服务器接收缓冲区在这里不相关,但是如果你设置它> = 64k你需要在侦听套接字上设置它,它将被接受的套接字继承,再次使握手可以协商窗口缩放。]

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. 它浪费了一个线程,一个线程堆栈,一个套接字,一个大型套接字发送缓冲区,一个FD,以及服务器上所有其他相关资源两个小时,并可能影响其他线程,从而影响其他客户端。 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. 在准备好开始接收数据之前,最好不要连接,或者在客户端读取并将数据假脱机到以后进行处理。

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

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