Select

Feb. 2, 2013, 7:53 p.m.
my $paddr = inet_aton($addr);
croak "Cannot resolve '$addr'" if (!defined $paddr);
socket SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp')
    or croak 'Cannot create socket';
connect SOCK, sockaddr_in($port, $paddr)
    or croak "Cannot connect to server '$server'";
send SOCK, "$query\r\n", 0;
vec($rin, fileno(SOCK), 1) = 1;
do {
    ($nfound) = select($rout = $rin, '', '', $timeout);
    croak 'Select() error' if ($nfound == -1);
    croak 'Timeout' if ($nfound == 0);
    croak 'Recv() error' if (!defined(recv SOCK, $buf, 512, 0));
    $answer .= $buf;
} while $buf ne '';
shutdown SOCK, 2;
comments powered by Disqus