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

I am working on a site for a rpg, my employer wants me to tell how many users are online and such, the server generates a htm doc located at xxxx.com/webstats/yyy.htm . All I want to do is open this file. The problem is I am not on the same machine as the file. I've tried using sockets but can't get it to work. I don't even Know if I'm doing it right. I'm using the
use IO::Socket; my $sock = IO::Socket::INET->new(PeerAddr => 'www.xxxxx.com', PeerPort => '81', Proto => 'tcp' ); while (<$sock>){ print; }
For some reason it either hangs up or doesn't print anything. Can anybody tell me whats wrong?

Andrew

Title edit by tye

Replies are listed 'Best First'.
Re: Sockets
by pfaut (Priest) on Jan 08, 2003 at 22:49 UTC

    Are you attempting to access a remote web page?

    use LWP::Simple; my $page = get('http://www.xxxxx.com:81/webstats/yyy.htm');

    See LWP::Simple for all the gorey details.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: Sockets
by Enlil (Parson) on Jan 08, 2003 at 22:52 UTC
    Maybe I am being naive but if all you are doing is requesting a page from a webserver why not use the LWP series of module(s) instead to retrieve the page which you can then parse through that.

    For example from lwpcook:

    use LWP::Simple; $doc = get 'http://www.linpro.no/lwp/';

    -enlil

Re: Sockets
by Jenda (Abbot) on Jan 08, 2003 at 23:43 UTC

    Just to prevent confusion

    1. HTTP is port 80, not 81. Of course you can set your server to use any other port, but 80 is the standard.
    2. The web server will not send you anything until you send it your request. You might want to read RFC 2068 (Hypertext Transfer Protocol -- HTTP/1.1) ... or not. I think it can't hurt to browse through the document a bit.

    Anyway ... it's much better to stick to LWP for what you need now.

    Jenda

Re: Sockets
by behroozi (Beadle) on Jan 09, 2003 at 00:35 UTC
    The other people who suggested using LWP are perfectly correct. If you still want to use sockets, your problem is that you are not sending the request properly to get the page. Try adding:
    print $sock "GET /webstats/yyy.htm HTTP/1.0\015\012\015\012";

    before trying to read from the socket*. For more information on the HTTP Spec (versions 1.1 and 1.0), check out W3C's HTTP page.

    *This isn't completely portable ;-). If on VMS, replace each \015\012 with \n, and if using EBCDIC, replace each \015\012 with "\r\n".