in reply to Clean closing sockets

I used to solve this problem in C using setsockopt(2), with the option SO_REUSEADDR. It basically tells the system that it's OK to bind to a port as long as no one is listening to the same port.

In Perl you can say:

setsockopt(YOURSOCKET, SOL_SOCKET, SO_REUSEADDR, 1) or die "your messa +ge\n";
-- TMTOWTDI

Replies are listed 'Best First'.
Re: Re: Clean closing sockets
by JPaul (Hermit) on Aug 16, 2001 at 00:19 UTC
    Okay, thats a fair enough answer. So my question is this, not to sound daft or anything:

    How do I setockopt() on a SOCKET when I can't generate the SOCKET because it won't bind to the port I'm telling it to?
    Clarify:

    my $server = IO::Socket::INET->new(....); setsockopt($server, SOL_SOCKET, SO_REUSEADDR, 1);
    $server doesn't exist until after the bind, but the bind won't happen (and perl will stop) at the ->new() because the port is already "taken"...
    I'm quite sure I'm so far off base you're laughing - which is fine - as long as you can tell me how its _supposed_ to work :)

    Thanks,
    JP

    -- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

      setsockopt can be used immediately after calling socket and before bind, if using the functional socket interface.

      With perldoc IO::Socket you can easily find out that all you need to do is passing the Reuse option to your constructor:

      my $server = IO::Socket::INET->new(...., Reuse => 1);

      Happy recycling!

      -- TMTOWTDI

        Like, doof.
        Thanks!

        JP
        -- Alexander Widdlemouse undid his bellybutton and his bum dropped off --