On Tue, Oct 13, 2015 at 6:34 AM, David Emanuel da Costa Santiago
<deman...@gmail.com> wrote:
> Hi!

Hello,

> THanks for your suggestion.

I assume this was off-list because I didn't get it. Also consider
bottom-posting or interleaved posting for easier reading.

> I changed my code to:
>
> sub _read_from_socket{
>   my ($select) = @_;
>
>   my ($output, $buffer) = ('', '');
>   my $socket = undef;
>   do {
>       $socket = ($select->can_read(0.1))[0];
>     }  while (!defined $socket);

You're essentially manually blocking your code here. Until you can
read the program will be stuck in that loop, blocked/hung. It's no
different than just reading without IO::Select (which probably wastes
fewer CPU cycles too).

In general it would depend on the protocol you're using. _Should_
there always be data when you go to read or could the remote end have
nothing to send you? You can try to rearrange your program to handle
the case when there is no data. Instead of looping until can_read(),
try printing or logging a diagnostic message and either moving on to
other work, putting the program to sleep to try again after some
period of time, or exit 1.

Perhaps your troubles aren't code and are instead network stability.
If your network isn't stable enough to receive the packets then there
isn't much you can do within software to fix it.

Unless you need socket-level access consider using a higher-level
network programming API to save yourself such low-level headaches. The
code to deal with sockets robustly is complicated and that wheel
already exists in several forms. Check CPAN for something more
appropriate.

Regards,


-- 
Brandon McCaig <bamcc...@gmail.com> <bamcc...@castopulence.org>
Castopulence Software <https://www.castopulence.org/>
Blog <http://www.bambams.ca/>
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'

-- 
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