[ macperl-Bugs-470321 ] alarm + connect never returns

[email protected]
Newsgroups perl.macperl.porters
Message-ID <[email protected]>
Bugs item #470321, was opened at 2001-10-11 10:55
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=107940&aid=470321&group_id=7940

Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Axel Rose (axelrose)
Assigned to: Nobody/Anonymous (nobody)
Summary: alarm + connect never returns

Initial Comment:
The following script tries to connect to two different hosts in order
to check reachability.

If the first connection is ok. the second will be properly reported
as reachable or not reachable.
If the first connection attempt fails the second connection test
will never return though.
The code itself should be correct I think. I get expected results
testing with *nix Perl.


#!/usr/local/bin/perl -w
use strict;
use Socket;
$|=1;

my( $host, $res );
$host = '172.16.100.1'; $res = port_check( $host, 407, 5 );
$res ? warn "ok: $host\n" : warn "cannot contact $host\n";
$host = '172.16.100.2'; $res = port_check( $host, 407, 5 );
$res ? warn "ok: $host\n" : warn "cannot contact $host\n";

sub port_check {
    my( $host, $port, $timeout ) = @_;
    $host ||= '127.0.0.1';
    $port ||= 21;
    $timeout ||= 5;

    local *SOCKET;
    socket( SOCKET, PF_INET, SOCK_STREAM, getprotobyname q(tcp) )
	or die "socket creation failed\n$!";
    # build the address of the remote machine
    my $iaddr = inet_aton( $host )
		or die "couldn't convert '$host' into an Internet address: $!\n";
    my $paddr = sockaddr_in( $port, $iaddr );

    local $SIG{'ALRM'} = sub { die "alarm" };
    if( eval { alarm $timeout; my $conn = connect( SOCKET, $paddr ) } ) {
		shutdown( SOCKET, 1 );
		close( SOCKET );
		alarm 0;
		return 1;
    }
    else {
		alarm 0;
		return 0;
    }
}


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=107940&aid=470321&group_id=7940
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.