perl:在while循环中连接

时间:2012-05-07 13:30:26

标签: perl sockets

我读了this,我会在while循环中调用connect来强制连接。 我可以做那样的事情,还是一个愚蠢的想法......

sub sendData2Master {
    my ($data) = @_ ;
    my $failed = -1;
    return( $failed ) unless defined $data ;

    my $proto = (getprotobyname('tcp'))[2] ;
    my $sin = sockaddr_in( $PORTMASTER, inet_aton($MASTER) ) ;
    my $FS;
    unless (socket($FS, AF_INET, SOCK_STREAM, $proto)) {
        logger("socket [$MASTER]:[$PORTMASTER] failed:[$?]:[$!]") ;
        return( $failed ) ;
    }

    while (!connect($FS, $sin) && ! $!{EISCONN}) 
    {
        if ($!{EINTR} || $!{EINPROGRESS})
        {
            last;
            # the connection shall be established asynchronously.
            # http://pubs.opengroup.org/onlinepubs/009695399/functions/connect.html
        }
        else
        {
            logger("connect to [$MASTER]:[$PORTMASTER] failed:[$?]:[$!]") ;
            if ($!{ECONNREFUSED})
            {
                # tcp server can refuse a connection because a low Listen parameter in IO::Socket::INET->new
                next;
            }
            else
            {
                return ( $failed );
            }
        }

    }

    select((select($FS),$|=1)[0]);
    unless ( print $FS "$data","\n" ) {
        logger("print [$data] to [$MASTER]:[$PORTMASTER] failed:[$?]:[$!]") ;
        return( $failed );
    }
    close ($FS) ;
    select((select(STDOUT),$|=1)[0]);

    return 1;
}

感谢任何已经这样做的建议或样本... ---------------------------------- --------------------------------------------

0 个答案:

没有答案