Buffer size for reading UDP packets in Python

I am trying to figure 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 it is? ~ 0.2 MBytes ..!?

However, if I look for the buffer size elsewhere, that is, on the command line:

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

.. I get 4096 bytes.

Try setting the size of the buffer, 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 happening?

Substituting SOL_SOCKET with SOL_UDP gives "Protocol Unavailable"

How to set up max. UDP packet size .. or even find it?

+6
source share
1 answer

I wanted to say how to know / adjust the buffer size

The way you did this with SO_RCVBUF was correct. But note that depending on your settings and your OS, you may get different values ​​with getsockopt than you set with setsockopt . The 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 by the way, are there network FIFO sockets? first discarded first when the buffer is saturated?

As far as I know, if a full buffer receive fails. It will not discard data already received but not processed to make room for new data.

+1
source

Source: https://habr.com/ru/post/982602/


All Articles