in reply to IO:: SOCKET Check Port, Please Help!

If you only want to find the first free port, you could just connect and see if that fails or use a combination with a timeout (see alarm) for the more complicated cases, e.g. where the server does connect but not respond.

use strict; use warnings; use IO::Socket::INET; sub get_sock_to_free_port { my ( $rpt_host, @rpt_ports ) = @_; foreach my $rpt_port ( @rpt_ports ) { my $sock = new IO::Socket::INET ( PeerAddr => $rpt_host, PeerPort => $rpt_port, Proto => 'tcp' ); return $sock if $sock; warn "failed check: $rpt_port - $!"; } die "No active port in list: @rpt_ports"; } my @rpt_ports = qw(12345 12346 22345 22346 22347 22348 22349 22350 22351 22352 22353 22354 22355 22356 22357 22358); my $sock = get_sock_to_free_port( '127.0.0.1', @rpt_ports ); ...

Update: (in response to AM feedback below): Premature end of script headers is a problem that can occur after a valid $sock has been found (protocol problem (\r\n?) / server problem / config problem). So it is not directly related to the solution given above. Perhaps checking the HTTPD server logs or the server-script helps?

Replies are listed 'Best First'.
Re^2: IO:: SOCKET Check Port, Please Help!
by Anonymous Monk on Sep 22, 2011 at 15:44 UTC
    This kinda looks like what I am looking for but it keeps failing. Premature end of script headers... any suggestion?