Sockets cannot tell you how much data will come in any language.  You HAVE
to rely on Content-Length, that is what it is for.  Why do you think "It
doesn´t
seem wise to rely [it]"?

It is not possible for a loop to both be blocking (which means it is using
no CPU until signaled there is data) and use 100% of CPU.

Why are you reading data directly from a socket yourself?  Especially if it
is HTTP data?  There are modules for this that handle all of the
intricacies for you.  For example, if you want to fetch a webpage, you
should say

use strict;
use LWP::UserAgent;
use warnings;

my $ua = LWP::UserAgent->new;

my $response = $ua->get("http://ifconfig.io/ip";);

unless ($response->is_success) {
    die "could not fetch IP address from ifconfig.io: ",
$response->status_line;
}

print $response->decoded_content, "\n";



On Sat, Aug 12, 2017 at 11:20 PM hw <h...@adminart.net> wrote:

> Hi,
>
> when reading data from a TCP socket, how do I tell when I have received
> all the data I should have received?
>
> Particularly, I´m trying to read data sent by a web server, so I´m
> opening a socket, send an HTTP request and receive an answer.
>
> Apparently I need to read from the socket in an endless loop.  I really
> don´t like that because it blocks the program and creates 100% CPU load,
> and I can´t tell if there is more data to be read or not.  It doesn´t
> seem wise to rely on the Content-Length: header to figure out whether I
> received all data or not.
>
> I guess I´m doing it all wrong because I couldn´t find a documentation
> about using sockets which isn´t anything but confusing and leaves lots
> of questions.  I also guess I need something event-driven, i. e. send a
> request, and in the event that the server sends data, somehow receive it
> in the background.
>
> The application would need to do a few things in the background because
> there would be several types of events.  Forking a process for every
> event doesn´t seem right, and it provides me with the problem of
> exchanging data between the various processes (at least unless I let
> every process do its job and exit and fork another one to do basically
> the same thing again).
>
> Is perl the wrong language to do this?  What would you suggest?
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

Reply via email to