Marcello has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a problem with my Perl server. Clients connect and send command terminated by a newline. The server uses IO::Select's can_read to determine if there's any data to read. If so, it reads all data up to the first newline.

The problem is when a client sends two or more commands at once. The server reads the first command, but after that can_read returns false although there is still data available. Why is can_read returning false when I only read a part of the total data (one command at a time)?

More info: it's a Linux machine, the perl server uses IO::Socket. Reading is done byte for byte, because commands can have different end characters (e.g. newline, ETX etc.)

Thanks for any help,

Marcel

Replies are listed 'Best First'.
Re: IO::Select can_read and windowing
by aquarium (Curate) on Jun 19, 2003 at 13:35 UTC
    from memory...when you access can_read and get a true it then automatically resets to false until your next client sends data...so, just keep on reading until eof or whatever from the socket once you have can_read = true.
      Thanks aquarium,

      But the problem is that I need to use blocking reads, so if I have read one command and issue another read, the server blocks until the next command is completely read. Is there a way to tell how many bytes can be read? I then can use this to determine if I should do another read? Or maybe another IO::Select function to determine if I've read all the data?

      Kind regards
      I quote man 2 select:
      Three independent sets of descriptors are watched. Those listed in readfds will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a file descriptor is also ready on end-of-file), those in writefds will be watched to see if a write will not block, and those in exceptfds will be watched for exceptions. On exit, the sets are modifiedin place to indicate which descriptors actually changed status.
      That seems to indicate behaviour as Marcello expected.

      Makeshifts last the longest.