Alejandro Santillan wrote:

> The $i reaches the number 57, before hanging. It seems that well before
> reaching
> the !END! string it reads some char undef or something.

You need to check $bytes for error and EOF before reading again.

>>>whereas:
>>>
>>>
>>>>>my $buffer;
>>>>>my $bytes = sysread ($handle, $buffer, 1024);
>>>>>if (not defined $bytes or $bytes < 0) {
>>>>>#err
>>>>>print "error!!!";
>>>>>} elsif ($bytes == 0) {
>>>>># EOF

print "EOF\n";

>>>>>}
>>>>>return $buffer;

The return would only be there if this is actually a subroutine.

> I have several questions here:
> I don't know what the EOF indicator should be. 

An EOF on the stream would be a 0 return from sysread.  Your !END!
pattern would be a logical EOF indication.

>                                                 I don't know how to read a
> second time, do I have to do select($handle) and then sysread again?

See below.

> What is to block?

Blocking is when you hang a read and there is no data coming in -
it will block your task from further execution until the read
completes or errs off.  You can avoid that by doing a select
until the select says there is data available or by using non-blocking
IO on the socket.  Non-blocking IO will return immediately from the
read even if there is no data (the return value will let you know if
there was data or not).

> In the first read I get:
> ^BF^D3765,4443734
> 
> !APID3765
> ^F^,938252,2 ,1386.25 ,FILL746419490 ,,|
> ^F^,938263,1 ,1385.75 ,FILL746428495 ,,|
> ^F^,938335,1 ,1386.25 ,FILL746498613 ,,|
> ^F^,938969,1 ,1385.50 ,FILL7471301676 ,,|
> ^F^,938972,1 ,1385.50 ,FILL7471331679 ,,|
> ^F^,939684,3 ,1389.50 ,FILL7478422788 ,,|
> ^F^,939684,1 ,1389.50 ,FILL7478422789 ,,|
> ^F^,939704,2 ,1754.75 ,FILL7478622814 ,,|
> ^F^,939704,1 ,1754.75 ,FILL7478622813 ,,|
> ^F^,939704,1 ,1754.75 ,FILL7478622812 ,,|
> 
> It lacks 3 or 4 lines.

How many characters is that ?  Doesn't look to be anywhere near your
8K read.  It must be coming in chunks of like 1K.  After you finish
one read, you can partially process whatever you can and then use
IO::Select::can_read before the next read (unless you don't mind
blocking).

Make sure you check for error (undef) and EOF (0) before reading again
or you could hang.  Make the code into a sub if it already isn't and
just keep calling it until it returns error, EOF or you get the EOR
pattern.

Untested code :

use strict;
use warnings;
use IO::Select;

# Your socket opening code here

my $read_set = new IO::Select();
$read_set->add($handle);

my $file = '';  # accumulated stream data

while (1) {

        my @ready = $read_set->can_read(0.1);

        # do any other stuff that you need before trying another read here

        next if not @ready;     # no data to read yet, loop back

        my $buffer = '';
        my $ret = read_data ($handle, \$buffer);
        if ($ret < 0) {
                print "Error reading socket\n";
        } elsif ($bytes == 0) {
                print "EOF on socket\n";
        } else {
                print "buffer=$buffer\n";
                $file .= $buffer;       # add to saved data
                my $found = index $file, '!END!;
                last if $found >= 0;    # break out of loop
        }
}

# finish processing data

exit;

sub read_data {
        my $fh = shift;
        my $bufref = shift;     # reference to read buffer
        my $len = shift || 8192;

my $bytes = sysread ($fh, $$bufref, 1024);
if (not defined $bytes or $bytes < 0) {
        return -1;
} elsif ($bytes == 0) {
        return 0;
}
return $bytes;

}

__END__
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to