Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Send Buffer Size for sockets in python

I have a client socket at my server end and what I want is to set Send buffer size for the socket just like I set Receive buffer size.Any idea on how I can set it? Because while sending huge data, the socket disconnects.

like image 570
Biswarup Dass Avatar asked Jun 17 '15 10:06

Biswarup Dass


People also ask

How do you size a socket buffer?

If you want see your buffer size in terminal, you can take a look at: /proc/sys/net/ipv4/tcp_rmem (for read) /proc/sys/net/ipv4/tcp_wmem (for write)

Do sockets have buffer?

A socket has two buffers and some other information associated with it. In the context of sockets programming, a socket is your app's interface to one TCP connection (or UDP flow). Your app doesn't read/write data from/to the network interface card (NIC) directly, it goes through the kernel's network stack.

What is send buffer size?

The size of the send buffer, in bytes. The default value is 8192 bytes.

How do I increase the socket buffer size in Linux?

Change the following set of parameters as needed: /proc/sys/net/core/rmem_default > recv > 124928 > changed to 512000. /proc/sys/net/core/wmem_default > send > 124928 > changed to 512000. /proc/sys/net/core/rmem_max > 124928 > changed to 512000.


1 Answers

Use socket.setsockopt() and SO_SNDBUF:

socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, <value>)      

Where <value> is the buffer size you want to set as a Python int.

Example:

socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 8192)  # Buffer size 8192

See: setsockopt

like image 61
James Mills Avatar answered Sep 20 '22 12:09

James Mills