Buffer size for reading UDP packets in Python

13,100

I wanted to say, how to find out / adjust the size of the buffer

The way you did with SO_RCVBUF was correct. But note that depending on your setting and on your OS you might get different values back with getsockopt than you set with setsockopt. On Linux socket(7) states:

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.

And, btw, are the network sockets FIFO ? first in - first discarded when the buffer gets saturated?

As far as I know if the buffer is full receiving will fail. It will not discard already received but not processed data to make room for new data.

Share:
13,100
El Sampsa
Author by

El Sampsa

In my previous life, I did computational materials physics at Spain and at Helsinki University. After that, I've been working at Dasys Oy, and lived in the crossroads of Python3, PyQt, SWIG and C/C++, designing and coding python programs with graphical user interfaces (using PyQt) that are capable of massive video streaming, with either libVLC and/or with libav (ffmpeg). At the moment, I'm developing an open source video management and analysis platform, check it out at github (the project name is "valkka-core").

Updated on June 26, 2022

Comments

  • El Sampsa
    El Sampsa almost 2 years

    I am trying to find out / adjust the size of network buffers:

    import socket
    
    sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    
    sock.getsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF)
    212992
    

    What on earth is this? ~ 0.2 MBytes ..!?

    However, if I am looking for the buffer size elsewhere, i.e. on the command line:

    sampsa@sampsa-xps13:~/python/sockets$ cat /proc/sys/net/ipv4/tcp_wmem
    4096    16384   4194304
    

    .. I get 4096 bytes.

    Let's try to set the buffer size and then check its value:

    sock.setsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF,1024)
    
    sock.getsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF)     
    2304
    

    What's going on?

    Substituting SOL_SOCKET with SOL_UDP gives "Protocol not available"

    How can I adjust the max. size of the UDP packet .. or even find it out?

    • Steffen Ullrich
      Steffen Ullrich about 9 years
      RCVBUF has not much to do with the maximum size of a UDP packet, but mainly how much data get buffered before they get discarded if the application is not fast enough to read them. Thus it is a socket property and not related to UDP. The max size of an UDP packet is 64k.
    • El Sampsa
      El Sampsa about 9 years
      Thanks .. well, uh.. that is exactly what I wanted to state. I wanted to say, how to find out / adjust the size of the buffer
    • El Sampsa
      El Sampsa about 9 years
      And, btw, are the network sockets FIFO ? first in - first discarded when the buffer gets saturated?
  • El Sampsa
    El Sampsa about 9 years
    1024 doubled ain't 2304
  • Steffen Ullrich
    Steffen Ullrich about 9 years
    Like I said, it does not need to reflect exactly what you set. Maybe it will set itself to a multiple of some kernel specific size.
  • Oleg Andriyanov
    Oleg Andriyanov about 9 years
    @SteffenUllrich "But note that depending on your setting and on your OS you might get different values back with getsockopt than you set with setsockopt" -- where is the documentation for such effect? Because man page for socket(7) is misleading -- it says nothing about the value "2304". I faced the same behaviour on Linux 3.16.0-30 (64 bit) -- found no way of setting buffer size down to the documented minimum of 256. 2304 is the lowest I have got with getsockopt.
  • Steffen Ullrich
    Steffen Ullrich about 9 years
    I don't know of any specific documentation. And it might differ between OS and maybe between different OS versions, kernel parameters etc.