#!/usr/bin/perl -w use strict; use Symbol; use Fcntl qw(:DEFAULT); use Socket; use Time::HiRes; print STDOUT simple("localhost", 80, "GET / HTTP/1.1\012Host:bidtxt.whenu.com\012\012"); exit 0; sub simple { my $host = shift || return undef; my $port = shift || return undef; my $mesg = shift || return undef; my $timeout = shift || 60; my $t0 = Time::HiRes::time(); my $ti; my $proto = getprotobyname('tcp'); my $iaddr = inet_aton($host) || return undef; my $paddr = sockaddr_in($port, $iaddr); my $s = Symbol::gensym; socket($s, PF_INET, SOCK_STREAM, $proto) || return undef; connect($s, $paddr) || return undef; my $fd = fileno($s); my $t; my $b; my $res; my $nf; my $tl; my $buf = "\000" x 8120; ## BUFSIZ my $l = length($mesg); my $rin = 0 ; my $win = 0 , my $ein = 0; my $rout = 0; my $wout = 0; my $eout = 0; vec($rin, $fd, 1) = 1; vec($win, $fd, 1) = 1; $ein = $rin | $win; my $i; $t = 0; $b = 0; while (($nf, $tl) = select(undef, $wout=$win, $eout=$ein, 2)) { $i++; $ti = Time::HiRes::time() - $t0; if ($ti > $timeout) { print STDERR "Timed out writing ($ti) $!\n"; shutdown($s, 2); return undef; } if (vec($wout, $fd, 1) == 1) { $b = syswrite($s, $mesg, $l - $b, $b); $t = $t + $b; last if $t = $l; } if (vec($eout, $fd, 1) == 1) { print STDERR "Got an error while writing: $!\n"; shutdown($s, 2); return undef; } } $t = 0; $l = 0; $b = 0; while (($nf, $tl) = select($rout=$rin, undef, $eout=$ein, 2)) { $i++; $ti = Time::HiRes::time() - $t0; if ($!) { print STDERR "ERROR $!\n"; } if ($ti > $timeout) { print STDERR "Timed out reading ($ti) $!\n"; shutdown($s, 2); return undef; } if (vec($rout, $fd, 1) == 1) { $b = sysread($s, $buf, 8192); $t = $t + $b; last if $b == 0; $res .= $buf; } if (vec($eout, $fd, 1) == 1) { print STDERR "Got an error while reading: $!\n"; shutdown($s, 2); return undef; } } shutdown($s, 2); return $res; }