in reply to Re^2: Fetching HTML Pages with Sockets
in thread Fetching HTML Pages with Sockets

odd, it works for me in perl 5.6.1 and 5.8.0 How about octal? "\015\012"

Replies are listed 'Best First'.
Re^4: Fetching HTML Pages with Sockets
by amt (Monk) on Sep 19, 2004 at 19:57 UTC
    The octal codes work, but the program still hangs. I have tested my HTTP statements by telnetting to the host and typing them in, the trouble I am having is with this darn socket.

    amt
      use Socket; use CGI; my $page = connect_host() or die "$!";; print "$page"; flush(SOCK); sub connect_host { my $remote = gethostbyname("football.fantasysports.yahoo.com") +; my $proto = getprotobyname('tcp'); my $port = 80; my $remote_host = sockaddr_in($port,$remote); socket(SOCK,PF_INET,SOCK_STREAM,$proto) or die "$!"; connect(SOCK, $remote_host); print SOCK "GET / HTTP/1.0\015\012\015\012"; my $html = <SOCK> if(defined(<SOCK>)) or die "$!"; return $html; } sub flush { select($_[0]); my $t=$|; $|=1; $|=$t; }
      New error message is Connection reset by peer at ./pcs.pl line 29.
      amt

        You flushed after the wrong print. <SOCK> is blocking because the GET is never sent; it's stuck in a buffer. flush forces the buffer to be sent and emptied, so the web server can see the request and reply to it.

        use Socket; sub connect_host { local *SOCK; my $remote = gethostbyname("football.fantasysports.yahoo.com") || die("Error resolving host name: $!\n"); my $proto = getprotobyname('tcp'); my $port = 80; my $remote_host = sockaddr_in($port, $remote); socket(SOCK, PF_INET, SOCK_STREAM, $proto) or die("Error creating socket: $!\n"); connect(SOCK, $remote_host) or die("Error connecting to remote host: $!\n"); print SOCK "GET / HTTP/1.0\015\012\015\012"; flush(SOCK); local $/; # Read until end of file (as opposed to end of line). return scalar(<SOCK>); } sub flush { select($_[0]); my $t=$|; $|=1; $|=$t; } my $page = connect_host(); print($page);