Aighearach has asked for the wisdom of the Perl Monks concerning the following question:

Hey, I'm trying to pre-open multiple sockets listening on the same address. (think apache in Perl...)

Problem is, when I set ReusePort => 1 I get Your vendor has not defined Socket macro SO_REUSEPORT, used at /usr/lib/perl5/5.6.1/IO/Socket/INET.pm line 160

Any clue what I can do for a workaround from here?

Perl version is 5.6.1 on i386 Linux

Code snippet is:

use strict; use warnings; use IO::Socket; my $sock = new IO::Socket::INET( Listen => SOMAXCONN, LocalPort => 5000, LocalAddr => 'localhost', Proto => 'tcp', ReuseAddr => 1, ReusePort => 1, ) or die "failed opening socket: $!";

--
Snazzy tagline here

Replies are listed 'Best First'.
Re: Problem with SO_REUSEPORT in IO::Socket::INET
by dws (Chancellor) on Apr 13, 2002 at 19:04 UTC
    when I set ReusePort => 1 I get Your vendor has not defined Socket macro SO_REUSEPORT

    A google search for "SO_REUSEPORT linux" produces a wealth of info. On quick read, it appears that

    1. SO_REUSEPORT appeared in 2.4.15 and later kernels,
    2. SO_REUSEPORT can only be used with multicast addresses, and
    3. SO_REUSEPORT and SO_REUSEADDR (and hence ReusePort and ReuseAddr) cannot be used together.

    YMMV on a deeper read.

Re: Problem with SO_REUSEPORT in IO::Socket::INET
by perlplexer (Hermit) on Apr 13, 2002 at 19:11 UTC
      Perhaps you could expand on what insights that provides, as he doesn't use IO::Socket in that article?
      --
      Snazzy tagline here
        Alright. From the code that you showed us, I see that you are trying to open multiple sockets listening on the same port. You can't do that even if you specify SO_REUSEPORT. Here is why
        ... For IP only one socket may be bound to a specific local address/port pair. For TCP a bound local socket endpoint (address/port pair) is unavailable for some time after closing the socket, unless the SO_REUSEADDR flag is set. Note that carelessly setting SO_REUSEADDR might make TCP more unreliable unless PAWS is used (see tcp(4)); the delay is needed to handle old pack- ets still in the network. ...
        He does not use IO::Socket, you are correct; however, he does show you how to implement a pre-forking server.
        That is what I was referring to when I posted that link.

        --perlplexer
Re: Problem with SO_REUSEPORT in IO::Socket::INET
by Necos (Friar) on Apr 14, 2002 at 00:58 UTC
    Another thing about the 'ReusePort' and 'ReuseAddr' that I'm not sure has been hit upon yet is that there is (I only remember this because I read it like 15 minutes ago) is that there is a single parameter called 'Reuse' that will allow your script/program to use the same port/address even if the script/program is stopped.

    Here is a snippet from the IO::Socket POD on this machine (Win2k, Perl 5.005_03):

    SUB-CLASSES
    IO::Socket::INET
    'IO::Socket::INET' provides a constructor to create an AF_INET domain socket and some related methods. The constructor can take the following options

    ...
    ...
    ...
    ...
    Reuse -- Set SO_REUSEADDR before binding
    Hope this is of some use to you...

    Note: One thing you will need to be careful of (this is a general networking practice...) is if anything is running on the same port that your program is running on. If so, you'll have a lot of problems. I don't remember whether or not you even need to have a 'ReusePort' parameter since, if I recall, you can't have multiple services bound to one port anyway (or maybe that comes from my lack of knowledge of sockets in the *nix/*nux domain).

    Theodore Charles III
    Network Administrator
    Los Angeles Senior High
    4650 W. Olympic Blvd.
    Los Angeles, CA 90019
    323-937-3210 ext. 224
    email->secon_kun@hotmail.com
    perl -e "map{print++$_}split//,Mdbnr;"
      Yeah... the "Reuse" param is a deprecated alias for ReuseAddr
      --
      Snazzy tagline here