Title: Another Sockets question
Jeremy,
 
You may want to take a look at
IO::Select->select
 
Hopes this helps...If you would like the complete code, just e-mail me.
 
Here is an example used in a perl NT service ( This is only a stub). This code is in the startup section.
 
 
    $sock = OpenConnection();
    $readable_handles = new IO::Select();
    $readable_handles->add($sock);
 
    while(ContinueRun()) {
        # The select() will block until a socket is ready to be read
        ($new_readable) = IO::Select->select($readable_handles,undef,undef,5);
 
        #print "Loop\n";
 
        # If it comes here, there is at least one handle to read from or
        # write to.
        foreach $temp_sock (@$new_readable) {
            if ($temp_sock == $sock) {
                $new_sock = $temp_sock->accept();
                # Got Connection Request, So send user Header, Prompt and
                # wait for input
                $readable_handles->add($new_sock);
                print "Got a Connection...\n";
            } else {
                # It is an ordinary client socket, ready for reading
                $bytes_read = sysread($temp_sock,$buf,1);
                if ($buf) {
                    $command = GetUserInput($temp_sock,$buf);
                    if ($command) {
                        # Move Cursor to beginning of next line of terminal
                        sendcrnewline($temp_sock);
                        ($shutdown,$connected) = ProcessCommand($command,$temp_sock);
                        sendcommandline($temp_sock);
                        }
                } else {
                    # Client closed socket. We do the same here, and remove
                    # it from the readable_handles list
                    $connection_cnt --;
                    print "Closed Connection (".$connection_cnt.")...\n";
                    $readable_handles->remove($temp_sock);
                    delete $User_Info{$temp_sock};
                    close($temp_sock);
                }
            }
        }
    }
 
    # Close the Listening Socket
    close ($sock);

Stephen Morgan
SYSTEMS ENGINEER
Z-Tel Communications, Inc.

404.504.7384 Phone
404.237.1167 Fax

[EMAIL PROTECTED]

-----Original Message-----
From: Goddard, Jeremy [mailto:[EMAIL PROTECTED]]
Sent: Monday, March 05, 2001 1:55 PM
To: Active Perl (E-mail)
Subject: Another Sockets question

Hi,

I'm trying to using non-blocking IO to get data.
But for some reason after all data is read the read blocks.
Any Ideas?


 for ($i=0; $i<$time_out; $i++)
  {
  sleep 1;
  $nread = sysread($main::socket, $answer, 1024);
  $rec .= $answer;
  print $answer;
  return $rec if ($rec =~ /\Q$pattern\E/);
  return 0 if ($nread==0);
  }

Thanks,

        Jeremy

Reply via email to