The problem is buffering - use IO::Handle's autoflush
method to flush data written to the socket immediately:
use Socket;
use IO::Handle;
$host="www.google.com";
socket(S,PF_INET,SOCK_STREAM,0);
S->autoflush(1);
$s=sockaddr_in(80,inet_aton($host));
if (connect(S,$s)) {
print "connected to $host\n";
print S "GET \/ HTTP\/1.0\n\n";
read(S,$data,100) || die;
close(S);
} else { die "connect failed\n" }
print $data;
But i wouldn't use that code. Try this instead:
use strict;
use Socket;
use IO::Handle;
my $data;
my $port = 80;
my $host = inet_aton('www.google.com') or die 'unkown host';
my $protocol = getprotobyname('tcp');
socket(SOCK,AF_INET,SOCK_STREAM,$protocol) or die "socket failed: $!";
my $dest_addr = sockaddr_in($port,$host);
connect(SOCK,$dest_addr) or die "connect failed: $!";
print STDERR "connected to host\n";
print SOCK "GET / HTTP/1.0\n\n";
SOCK->autoflush(1);
read(SOCK,$data,100);
close(SOCK);
print $data;
Even better - use IO::Socket:
use strict;
use IO::Socket;
my $data;
my $port = 80;
my $host = 'www.google.com';
my $socket = IO::Socket::INET->new(
Proto => 'tcp',
PeerAddr => $host,
PeerPort => $port,
) or die "socket failed: $@";
$socket->autoflush(1);
print STDERR "connected to host\n";
print $socket "GET / HTTP/1.0\n\n";
$socket->recv($data,100);
print $data;
I highly recommend the book,
Network Programming with Perl. Pure
gold. :)
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
|