> 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__
Bill, this code you wrote is just precious. I think I won't arrive to write it in a million years. Besides, I was able to learn a lot from it, to correct some minor mistakes and to test it. It works perfect now. For the records, I type below the final code. Thank you! #!/usr/bin/perl #this code reads a socket that sends a message in chunks #as the message has as terminator string !END! it will end its work #when that string is read. Here it is: #use strict; use warnings; use IO::Select; # Your socket opening and request code here ConnectAndRequest(); $finalreading=readthesocket(); print $finalreading; exit; ########### SUB PART ############ sub readthesocket{ sub leomas(){ 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 ($ret == 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 return $file; } 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 SUB PART ############ _______________________________________________ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs