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?


In reply to Re: IO:: SOCKET Check Port, Please Help! by Perlbotics
in thread IO:: SOCKET Check Port, Please Help! by rizcotech

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.