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)

In reply to (jeffa) Re: sockets in XP activeperl by jeffa
in thread sockets in XP activeperl by cghost

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.