問題描述
我正在使用 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 數據在發送方和接收方都被緩沖.接收方的socket接收緩沖區的大小決定了在沒有確認的情況下可以傳輸多少數據,發送方的發送緩沖區的大小決定了在發送方阻塞或獲得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.]
然而,我完全同意 Martin James 的觀點,即這是一個愚蠢的要求.它在服務器上浪費一個線程、一個線程堆棧、一個套接字、一個大套接字發送緩沖區、一個 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 套接字緩沖區的最大大小?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!