简体   繁体   English

设置接受套接字的缓冲区大小

[英]Setting buffer size of accepted sockets

What is the correct way to set the buffer (ie., SO_RCVBUF, SO_SNDBUF) size of an accept()'d socket under Linux?在 Linux 下设置 accept() 套接字的缓冲区(即 SO_RCVBUF、SO_SNDBUF)大小的正确方法是什么?

The obvious answer would be to call setsockopt() on the newly created socket, however the tcp man page states:显而易见的答案是在新创建的套接字上调用 setsockopt(),但是 tcp 手册页指出:

On individual connections, the socket buffer size must be set prior to the listen(2) or connect(2) calls in order to have it take effect.在单个连接上,必须在 listen(2) 或 connect(2) 调用之前设置套接字缓冲区大小才能使其生效。 See socket(7) for more information.有关更多信息,请参阅 socket(7)。

That makes sense as the buffer is likely allocated upon creation and therefor I'll have to rely on the inheritance semantics of the listening socket by setting it's buffer size, except the man page (nor that of socket) makes any mention of inheritance and actually states:这是有道理的,因为缓冲区可能是在创建时分配的,因此我必须通过设置缓冲区大小来依赖侦听套接字的继承语义,除了手册页(或套接字的手册页)没有提到继承,实际上状态:

On Linux, the new socket returned by accept() does not inherit file status flags such as O_NONBLOCK and O_ASYNC from the listening socket.在 Linux 上,accept() 返回的新套接字不会从侦听套接字继承文件状态标志,例如 O_NONBLOCK 和 O_ASYNC。 This behavior differs from the canonical BSD sockets implementation.此行为不同于规范的 BSD 套接字实现。 Portable programs should not rely on inheritance or noninheritance of file status flags and always explicitly set all required flags on the socket returned from accept()可移植程序不应依赖于文件状态标志的继承或非继承,并且始终在从 accept() 返回的套接字上显式设置所有必需的标志

It's not clear what "file status flags" refer to and whether or not it's inclusive of socket options and while having read a few related stackoverflow questions I'm none the wiser.目前尚不清楚“文件状态标志”指的是什么以及它是否包含套接字选项,虽然阅读了一些相关的 stackoverflow 问题,但我并不聪明。

That first quotation only applies to the receive buffer, and the real reason is so that a window scale can be negotiated during the connect handshake if it is >= 64k.第一个引用仅适用于接收缓冲区,真正的原因是如果> = 64k,则可以在连接握手期间协商窗口比例。 The answer is indeed to set it on the listening socket, from where it will inherited by all accepted sockets, and no, it isn't a file status flag.答案确实是将它设置在侦听套接字上,从那里它将被所有接受的套接字继承,不,它不是文件状态标志。

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

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