> From: Gary
> 
> I wrote:
> 
> > After sending a request to a server [...] I get some part of the
data
> > back that I requested, and then only some time later, after my code
> > thinks it's got everything, has continued and is trying to receive
> > returned data from a subsequent request, does it get the missing
part
> > of the first set of data.
> 
> For the record, this is what I did to get around the problem:
> 
> ,----[ code ]
> | my $s = IO::Select->new($self->{_sock}); while (my @ready =
> | $s->can_read(1)) {
> |     my $recd = RECV_BUF_SIZE;
> |
> |     while ($recd == RECV_BUF_SIZE) {
> |         my $buf = '';
> |
> |         $self->{_sock}->recv($buf, RECV_BUF_SIZE, MSG_WAITALL);
> |
> |         $recd = length($buf);
> |         $ret .= $buf;
> |     }
> | }
> `----
> Note the added 'while' loop around the recv loop.
> 
> I can only say "it seems to work". I'm not really au fait enough with
this to say
> that it's a solution.

Ok, this will append all of the full buffers and the first partial
buffer into a single string ($ret). If you are going to handle multiple
passes, make sure $ret is empty each time you enter the loop. It
probably should be declared and initialized to an empty string anyway.

   my $ret = "";

Is there anything constant about your incoming messages? Are they all
the same length, or is there a delimiter at the end you can test for? If
the length is constant, I would change the loop test to (length($ret) <
SIZE) instead of just waiting for a partial buffer.

If you do get to handle multiple messages without a significant delay
between them, this code also might produce two messages in $ret, or part
of a second appended to the first.

Bob McConnell


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