cghost has asked for the wisdom of the Perl Monks concerning the following question:

hi, i recently migrated to XP, and the following chunk of very straight forward code dies at the read() on line 11 every time i run it. it connects then dies. i tried replacing the read() with @data=<S>, but the array always ends up empty. i tried the exact same http request over telnet, and it worked fine.
#!/usr/bin/perl use Socket; $host="www.google.com"; socket(S,PF_INET,SOCK_STREAM,0); $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;
i have no idea why this wouldnt work. thanks

Replies are listed 'Best First'.
(jeffa) Re: sockets in XP activeperl
by jeffa (Bishop) on May 21, 2002 at 05:48 UTC
    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)
    
Re: sockets in XP activeperl
by abstracts (Hermit) on May 21, 2002 at 05:17 UTC
    Hello

    Where is your debugging code? I mean: perl gives you all the means of tracking down the errors, yet I don't see you used any of them. Let me explain:

    • You need to put #!/usr/bin/perl -w in the first line to get warnings.
    • You should use strict.
    • You should always check return values.
    • When an error occurs, print out the error (using $!):
      if (connect(S,$s)) { print "connected to $host\n"; print S "GET \/ HTTP\/1.0\n\n"; read(S,$data,100) || die "Error Reading: $!\n"; close(S); } else { die "connect failed: $!\n" }
    • Read the perldocs carefully as the socket function clearly states: You should "use Socket", and the read page states: Returns the number of bytes actually read, 0 at end of file, or undef if there was an error. . So, at the read line, you should do my $lines = read(...); die "Error Reading: $! \n" unless defined $lines;
    • Check out the LWP and LWP::Simple for doing HTTP get, and save yourself the headache.
    • If you really want to do raw protocol level communication, then use the IO::Socket::INET for a clearer more OO interface to sockets.
    Hope this helps,,,