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

i'm sure this is a newbish question, but oh well this is getting under my skin. i'm using IO::Socket to make raw http requests to a webserver and pull documents. this is fine and dandy, except waiting for the socket to time out after all data is received gets a bit annoying. eof() doesn't work obviously...and not every document has nice neat closing tags for me to work with...i know there are other modules to work with, but shouldn't there be a way to accomplish this with IO::Socket? much thanks in advance.

Replies are listed 'Best First'.
Re: network socket EOF
by dws (Chancellor) on Jul 19, 2003 at 07:08 UTC
    .. except waiting for the socket to time out after all data is received gets a bit annoying.

    Which is a reason to use LWP::UserAgent or LWP::Simple. Part of the reason these modules exist is to deal with HTTP requests, including honoring the Content-length header in an HTTP response, which informs the client of the number of bytes of response to read (since the socket may be held open for subsequent requests). You can do this yourself on a raw socket, but it's a lot of work that these modules already do.

      ahh i guess i still haven't grown used to incorporating prefabbed code...lwp works very well though, and my script is quite a lot cleaner :) thanks again to both of you.
Re: network socket EOF
by bobn (Chaplain) on Jul 19, 2003 at 07:07 UTC

    If IO::Socket doesn't have a timeout option for the operation you're doing, look at perldoc -f alarm - but wouldn't it really be easier to use LWP::Simple or LWP::UserAgent?

    -Bob Niederman, http://bob-n.com
Re: network socket EOF (HTTP 1.0/1.1)
by tye (Sage) on Jul 20, 2003 at 04:43 UTC

    I know you already have a solution (using a module). But, just in case someone was curious about this problem...

    If you make a HTTP 1.0 request, then you'll get EOF at the end of the data. If you make a HTTP 1.1 request, then you'll get a header telling you how many bytes to read to complete the request. (The protocol used is specified in the headers you send to the server.)

    Even if you make an HTTP 1.1 request, you can terminate it by sending EOF to the server using the shutdown function which should cause the server to send back an EOF at the end of the last response.

                    - tye