理解/ SO_SNDBUF的getsockopt [英] Understanding set/getsockopt SO_SNDBUF
问题描述
我有以下程序检查发送缓冲区大小UDP套接字。然而,我返回值是有点混乱给我。我用下面简单的应用程序:
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\n", sendbuff);
// Set buffer size
sendbuff = 98304;
printf("sets the send buffer to %d\n", 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\n", sendbuff);
return 0;
}
我的机器上的输出是:
The output on my machine is:
发送缓冲区的大小= 129024
send buffer size = 129024
设定发送缓冲区为98304
sets the send buffer to 98304
发送缓冲区的大小= 196608
send buffer size = 196608
任何人都可以清楚我在做什么错在这里或如何跨preT输出?
Can anybody clarify what I'm doing wrong here or how to interpret the output?
推荐答案
你没有做错什么。 Linux的双打值(在内核中),当你设置它,当你查询它的返回值增加了一倍。 7人座
说:
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.
[...]
这篇关于理解/ SO_SNDBUF的getsockopt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!