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

I am opening a listener with:
my $lis = IO::Socket::INET->new(LocalPort => $portnumber, Listen => 10 );

When i restart it, I get the "Can't make server socket: IO::Socket::INET: Address already in use" message. So i tried closeing the socket with:
$lis->close();

But am still getting the message. How do I close it off so that I don't get the already in use message?

Replies are listed 'Best First'.
•Re: Closing IO::Socket::INET
by merlyn (Sage) on Sep 09, 2003 at 14:06 UTC
    Add ReuseAddr => 1 to your param list. The OS is trying to help you by not letting any lingering packets in a connection accidently go to a new process. If you are sure that this will never happen (or don't care), you can tell the kernel that you'll live dangerously by permitting quick re-use of the same port number.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Closing IO::Socket::INET
by vek (Prior) on Sep 09, 2003 at 18:33 UTC

    You can use the Reuse argument in the IO::Socket::INET constructor:

    my $lis = IO::Socket::INET->new(LocalPort => $portnumber, Reuse => 1, Listen => 10 );

    Doing this will set the SO_REUSEADDR option on the new socket. Without it, you'll have to wait a few minutes between exiting & restarting.

    -- vek --