Buffer size for reading UDP packets in Python

2024/10/14 22:18:11

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

import socketsock = 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?

Answer

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.

https://en.xdnf.cn/q/69362.html

Related Q&A

Why does datetime give different timezone formats for the same timezone?

>>> now = datetime.datetime.now(pytz.timezone(Asia/Tokyo)) >>> dt = datetime(now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond, pytz.timezone(Asia/Tokyo)…

Connect with pyppeteer to existing chrome

I want to connect to an existing (already opened, by the user, without any extra flags) Chrome browser using pyppeteer so I would be able to control it. I can do almost every manual action before (for …

Combining asyncio with a multi-worker ProcessPoolExecutor and for async

My question is very similar to Combining asyncio with a multi-worker ProcessPoolExecutor - however a slight change (I believe its the async for) makes the excellent answers there unusuable for me.I am …

Convert UTF-8 to string literals in Python

I have a string in UTF-8 format but not so sure how to convert this string to its corresponding character literal. For example I have the string:My string is: Entre\xc3\xa9Example one:This code:uEntre\…

Memory usage not getting lowered even after job is completed successfully

I have a job added in apscheduler which loads some data in memory and I am deleting all the objects after the job is complete. Now if I run this job with python it works successfully and memory drop af…

How to output sklearn standardscaler

I have standardized my data in sklearn using preprocessing.standardscaler. Question is how could I save this in my local for latter use?Thanks

How to use Jobqueue in Python-telegram-bot

I have able to make a bot very easily by reading the docs but Jobqueue is not working as per it is written. The run_daily method uses a datetime.time object to send the message at a particular time but…

Is there a way to override default assert in pytest (python)?

Id like to a log some information to a file/database every time assert is invoked. Is there a way to override assert or register some sort of callback function to do this, every time assert is invoked?…

How to install pycairo on osx?

I am trying to install the pycairo (Python bindings for the cairo graphics library) under OSX.I started witheasy_install pycairoand got: Requested cairo >= 1.8.8 but version of cairo is 1.0.4error: …

How do I change a value in a .npz file?

I want to change one value in an npz file.The npz file contains several npys, I want all but one ( run_param ) to remain unchanged and I want to save over the original file.This is my working code:DATA…