了解 set/getsockopt SO_SNDBUF 大小加倍 [英] Understanding set/getsockopt SO_SNDBUF size doubles

查看:23
本文介绍了了解 set/getsockopt SO_SNDBUF 大小加倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下程序来检查 UDP 套接字的发送缓冲区大小.但是,我的返回值让我有点困惑.我使用以下简单的应用程序:

#include #include int main(int argc, char **argv){int sockfd,sendbuff;socklen_t optlen;sockfd = 套接字(AF_INET,SOCK_DGRAM,0);如果(sockfd == -1)printf("错误");int res = 0;//获取缓冲区大小optlen = sizeof(sendbuff);res = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, &optlen);如果(res == -1)printf("getsockopt 错误一");别的printf("发送缓冲区大小 = %d
", sendbuff);//设置缓冲区大小sendbuff = 98304;printf("设置发送缓冲区为 %d
", sendbuff);res = setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, sizeof(sendbuff));如果(res == -1)printf("错误setsockopt");//获取缓冲区大小optlen = sizeof(sendbuff);res = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, &optlen);如果(res == -1)printf("getsockopt 错误二");别的printf("发送缓冲区大小 = %d
", sendbuff);返回0;}

我机器上的输出是:

发送缓冲区大小 = 129024

设置发送缓冲区为 98304

新的发送缓冲区大小 = 196608

谁能澄清我在这里做错了什么或如何解释输出?

解决方案

你没有做错任何事.Linux 在您设置时(在内核中)将该值加倍,并在您查询时返回加倍后的值.man 7 socket 说:

<前>[...]SO_SNDBUF设置或获取最大套接字发送缓冲区(以字节为单位).内核-nel 将此值加倍(为簿记开销留出空间)当它使用setsockopt()设置时,这个加倍的值是由getsockopt() 返回.默认值由wmem_default sysctl 和最大允许值由wmem_max sysctl.此选项的最小(加倍)值为2048.[...]笔记Linux 假定发送/接收缓冲区的一半用于内部内核结构;因此 sysctls 是可以观察到的两倍电线.[...]

Hi I have the following program to check the send buffer size for a UDP socket. However, I the return value is a bit confusing to me. I use the following simple app:

#include <sys/socket.h>
#include <stdio.h>

int main(int argc, char **argv)
{
 int sockfd, sendbuff;
 socklen_t optlen;

 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
 if(sockfd == -1)
     printf("Error");

 int res = 0;

 // Get buffer size
 optlen = sizeof(sendbuff);
 res = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, &optlen);

 if(res == -1)
     printf("Error getsockopt one");
 else
     printf("send buffer size = %d
", sendbuff);

 // Set buffer size
 sendbuff = 98304;

 printf("sets the send buffer to %d
", sendbuff);
 res = setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, sizeof(sendbuff));

 if(res == -1)
     printf("Error setsockopt");


 // Get buffer size
 optlen = sizeof(sendbuff);
 res = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, &optlen);

 if(res == -1)
     printf("Error getsockopt two");
 else
     printf("send buffer size = %d
", sendbuff);

 return 0;
}

The output on my machine is:

send buffer size = 129024

sets the send buffer to 98304

new send buffer size = 196608

Can anybody clarify what I'm doing wrong here or how to interpret the output?

解决方案

You're not doing anything wrong. Linux doubles the value (within the kernel) when you set it, and returns the doubled value when you query it. man 7 socket says:

[...]

    SO_SNDBUF
              Sets or gets the maximum socket send buffer in bytes.  The  ker-
              nel doubles this value (to allow space for bookkeeping overhead)
              when it is set using setsockopt(), and  this  doubled  value  is
              returned  by  getsockopt().   The  default  value  is set by the
              wmem_default sysctl and the maximum allowed value is set by  the
              wmem_max sysctl.  The minimum (doubled) value for this option is
              2048.
[...]

NOTES
       Linux assumes that half of the send/receive buffer is used for internal
       kernel structures; thus the sysctls are twice what can be  observed  on
       the wire.
[...]

这篇关于了解 set/getsockopt SO_SNDBUF 大小加倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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