Understanding set/getsockopt SO_SNDBUF size doubles

后端 未结 1 1991
天涯浪人
天涯浪人 2020-12-08 01:53

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:

相关标签:
1条回答
  • 2020-12-08 02:34

    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.
    [...]
    
    0 讨论(0)
提交回复
热议问题