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;
sub sendRaw($$$) {
    use constant IPPROTO_RAW => 255;
    my ($destination, $dport, $packet) = @_;
    eval {
        socket(SOCKET, PF_INET, SOCK_RAW, IPPROTO_RAW);
        my $paddr = sockaddr_in($dport, inet_aton($destination));
        send(SOCKET, $packet, 0, $paddr);
    };
    if ($@) {
        print "Error occured: $@";
    }
}

sub assembleRawUdp($$$$$) {
    my ($destination, $dport, $source, $sport, $data) = @_;

(Read more)