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

I'm trying to write a server for doing some things remotely on another computer. I start the server, and it waits for me to connect, then use a continuous loop for the duration until a condition breaks it.
my $server = IO::Socket::INET->new(LocalPort => 666, Type=> SOCK_STREAM, Listen => 0, Proto => "tcp"); die "Can't open socket" if (!$server); [...the main loop...] close($server); log_msg("Connection lost.\n");
I added the close() because when the program ends by itself this way (as opposed to being terminated with ^C), the port is evidently left blocked -- when I try to restart

Can't open socket

Which I take that to mean the port is not available. This lasts about a minute then the port is free again. Why would it be freed by ^C but not by close()?

The OS is linux.

Replies are listed 'Best First'.
Re: closing socket/port
by Fletch (Bishop) on Aug 14, 2009 at 17:44 UTC

    You probably want to pass ReuseAddr => 1 to the constructor (and if you want the why google for SO_REUSEADDR).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Thanks!
Re: closing socket/port
by rcaputo (Chaplain) on Aug 14, 2009 at 18:15 UTC
      Can't open socket: Address already in use