#!/usr/bin/perl
require 'sys/socket.ph';
$host = 'www.fc-lab.com';
$port = 80;
$resouce = '/index.html';
$destfile = 'result.html';
sub connect_server {
local($handle,$host,$port) =@_;
local($ip_address,$server);
local($my_port);
local($proto) = (getprotobyname('tcp'))[2];
if ($host =~ /^$s((\d+\.){3}\d+)\s*$/) {
$ip_address = pack('C4',split(/\./,$host));
} else {
$ip_address = (gethostbyname($host))[4];
}
return 0 unless $ip_address;
if ($verbose) {
($net,$host,$ho,$i) = unpack("C4",$ip_address);
printf "connect to %d.%d.%d.%d : %d \n",$net,$host,$ho,$i,$port;
}
$server = pack('S n a4 x8', &AF_INET,$port,$ip_address);
socket($handle,&PF_INET,&SOCK_STREAM,$proto) || die "socket : $!";
connect($handle,$server) || die "connect : $!";
select($handle);
$|=1;
select(STDOUT);
}
&connect_server('IN',$host,$port);
print IN "GET $resource\n";
$str=join("",(">",$destfile));
open(FOUT,$str);
while(<IN>) {
print FOUT $_;
}
close(FOUT);
|