Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

How to increase Socket buffer size?

by sureshr (Beadle)
on May 06, 2003 at 05:43 UTC ( #255804=perlquestion: print w/replies, xml ) Need Help??
sureshr has asked for the wisdom of the Perl Monks concerning the following question:

I have an UDP application with no flow control and so, can't afford packet loss. It so happens that I tend to miss packets over time (though I have separate read threads, etc). So, is there a way in Perl, by which I can increase the socket buffer size (memory is not a problem for me)? I checked the Perl docs of IO::Socket::INET & it does'nt seem to have an obvious option for this.
ps: I would prefer not to re-compile Perl for this purpose (I think some header's #define can do this). I am Perl5.8 on win2k.

Replies are listed 'Best First'.
Re: How to increase Socket buffer size?
by chromatic (Archbishop) on May 06, 2003 at 06:18 UTC

    How did you determine that you're losing packets due to buffer overflows? There are a lot of things that can go wrong between two hosts; how did you narrow it down? UDP doesn't guarantee package delivery; that's why they call it Unreliable. :)

    If reliability is that important to you, you could implement packet numbers, reassembly for out-of-order packet delivery, and retransmission guidelines. Of course, it may end up looking a lot like TCP. Would it be possible to switch protocols?

      Ofcourse, I have flow sequence number in the packed data with which I can determine the packet loss. If the packet is lost in the n/w then I am sure we can't help. But increasing the socket buffer size is one of the measures that we can take to avoid the packet loss, to handle situations when there is resource problem on the system. I agree that this would not necessarily mean the packet loss would be eliminated -- the very reason for having something like TCP :) I was just thinking of a preventive measure.
Re: How to increase Socket buffer size?
by pzbagel (Chaplain) on May 06, 2003 at 06:24 UTC

    Actually, if your application uses UDP then it is most likely designed to tolerate some packet loss. That is the whole point of UDP. With TCP you get handshakes, checksums, acknowledgements, but you pay for it with some overhead and sacrificing some speed. With UDP you get none of that, instead you get a fast protocol with low overhead. At the same time your client program sends it's packets and doesn't care if they get there or not.

    Perhaps you need to rethink your strategy regarding this UDP application. Dropping some packets may not be so bad in the long run. Or what about rewriting the socket code in C so it can suck down the packets faster?

    Hope this gives you some ideas.


      I am fine with losing 'some packets', but the whole idea of increasing the buffer size is to 'minimize' the packet loss, which I am sure would help during peak resource utilization of the system.
Re: How to increase Socket buffer size?
by perlplexer (Hermit) on May 06, 2003 at 14:01 UTC
    On UNIX you'd use setsockopt() and SO_RCVBUF/SO_SNDBUF to set the size of the receive/send buffers. I don't know if this is going to work on Windows but it's worth a try...

Re: How to increase Socket buffer size?
by RMGir (Prior) on May 06, 2003 at 14:12 UTC
    Wild guesses follow, so you've been warned.

    In C (or C++) on Unix (and maybe Win32? Not sure), you'd do this by doing a

    int newMaxBuff=512000; setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &newMaxBuff, sizeof(newMaxBuff)) +;
    IO::Socket has setsockopt and sockopt methods. Have you tried setting SO_RCVBUF using those methods? Using getsockopt, you could at least check if using setsockopt changed _something_.

    As I said, wild guesses...

      perlplexer & mike,

      $sock->sockopt(SO_RCVBUF, $max_recv_buff_sz);

      works for me. gr8
      where $sock is a IO::Socket::INET object. SO_RCVBUF is defined in  INET seems to be derived from IO::Socket which derives from Socket. So ideally, one should be able to set the socket buffer size of a socket created using Socket class & its method setsockopt. In my case sockopt<\code> method calls <code>setsockopt internally, by deriving.

      Thanks ppl,
      Suresh R
Re: How to increase Socket buffer size?
by valdez (Monsignor) on May 06, 2003 at 08:24 UTC

    Look at Listen option of IO::Socket::INET; please note that if you are using the constant value SOMAXCONN, you are already using the maximum number of pending connections available for your system.

    update: my answer is totally wrong.

    Sorry, Valerio

      I think you are referring to increasing the number of connections that can be accepted, rather than increasing the socket buffer size...
Re: How to increase Socket buffer size?
by Anonymous Monk on May 06, 2003 at 12:56 UTC
    It appears you will have to recompile Socket, as well as know what to modify (it's not apparent) to affect the buffer size
Re: How to increase Socket buffer size?
by ibanix (Hermit) on May 07, 2003 at 13:40 UTC
    can't afford packet loss

    Use TCP? That's what it was designed for. Fault-tolerant transmission.

    $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://255804]
Approved by jkahn
Front-paged by halley
erix just did a 12 km-run. Slow, but unstoppable!
[choroba]: My recent record is 10km in 1 hour :)
[perldigious]: Nice erix... perldigious' legs are so sore from portaging his kayak from place to place last weekend he can barely hobble 12m. :-)
[erix]: you're faster than me then :)
[erix]: well, at the moment, that is :)
[erix]: I should lose some weight although I keep telling myself that I'm doing extra training by lugging it along
[erix]: I saw a common kingfisher and a couple of bullfinches
[erix]: bullfinch ( a male and a female )
erix learned the word 'portaging' not so long ago (from vikings lugging their ships from one river-system to another)

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2017-05-24 12:44 GMT
Find Nodes?
    Voting Booth?