Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Clean closing sockets

by JPaul (Hermit)
on Aug 15, 2001 at 23:35 UTC ( [id://105155]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings all,

I'm writing a sockets server and am having problems doing what apparently should be a relatively easy thing.
Whenever I terminate the server (I trap SIGINT), I do a
close($server);
However, if I immediately attempt to restart my server the port is still bound and I have to wait for Linux to clear the port before I can redo the bind. Should I be doing something more flamboyant to $server than simply close()ing it?

Cheers all,
JP

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

Replies are listed 'Best First'.
Re: Clean closing sockets
by traveler (Parson) on Aug 15, 2001 at 23:52 UTC
    Use setsockopt and set the option SO_REUSEADDR:
    use Socket; ... setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR) or warn "OOPS, setsockopt failed $!\n";
    HTH, --traveler
Re: Clean closing sockets
by trantor (Chaplain) on Aug 15, 2001 at 23:58 UTC

    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
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://105155]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-25 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found