gpt4 book ai didi

c++ - getsockopt() 返回的值是之前由 setsockopt() 设置的值的两倍

转载 作者:太空宇宙 更新时间:2023-11-04 10:03:27 26 4
gpt4 key购买 nike

我正在尝试增加用于与 Linux 设备驱动程序交互的原始套接字的 SO_RCVBUF。默认的 rmem_default/rmem_max 都太小了,163840。因此我使用以下 stack overflow question/answers帮我。一切正常,或者至少看起来像这样。但是,当我得到我为 SO_RCVBUF 设置的值时,它返回我设置的值 * 2?谁知道这是为什么?

int recv_val = SOCK_RCV_BUF_MAX; socklen_t size = sizeof(recv_val);

if(setsockopt(sock_fd, SOL_SOCKET, SO_RCVBUF, &recv_val, size) < 0)
{
fprintf(stderr, "Error setsockopt(SO_RCVBUF): %s\n", strerror(errno));
}
else
printf("Set the SO_RCVBUF to %d\n", recv_val);

recv_val = 0;

if (getsockopt(sock_fd, SOL_SOCKET, SO_RCVBUF, &recv_val, &size) < 0)
{
fprintf(stderr, "Error getsockopt(SO_RCVBUF): %s\n", strerror(errno));
}
else if(recv_val == SOCK_RCV_BUF_MAX)
{
printf("Successfully set the buffer max to %d\n", SOCK_RCV_BUF_MAX);
}
else
printf("Failed to set the buffer to max (%d), val = %d\n", SOCK_RCV_BUF_MAX, recv_val);

输出

Set the SO_RCVBUF to 64000000
Failed to set the buffer to max (64000000), val = 128000000

更改为 recv_val = SOCK_RCV_BUF_MAX/2 输出

Set the SO_RCVBUF to 32000000
Successfully set the buffer max to 64000000

如果我不使用 setsockopt() 设置值并为我的套接字调用 getsockopt(),我会得到正确的默认值

Failed to set the buffer to max (64000000), val = 163840

最佳答案

您给 setsockopt(SO_RCVBUF) 的值只是一个提示,而不是绝对值。如果愿意,套接字提供者可以使用不同的值。您从 getsockopt(SO_RCVBUF) 得到的是使用的实际值。

您所看到的实际上是记录在案的行为:

http://man7.org/linux/man-pages/man7/socket.7.html

SO_RCVBUF
Sets or gets the maximum socket receive buffer in bytes. The kernel doubles this value (to allow space for bookkeeping overhead) when it is set using setsockopt(2), and this doubled value is returned by getsockopt(2). The default value is set by the /proc/sys/net/core/rmem_default file, and the maximum allowed value is set by the /proc/sys/net/core/rmem_max file. The minimum (doubled) value for this option is 256.

关于c++ - getsockopt() 返回的值是之前由 setsockopt() 设置的值的两倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54314735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com