Hello!

I'm trying to learn about network programming and sockets in perl. I'm
fairly new to the language, I just read Randal's book.

What I'm trying to do is a simple program that reads every line of <STDIN>
from multiple clients and prints them on real time to <STDOUT> on a server.

After a lot of googling, struggling with threads and fork, I ended up on the
perl-doc page for IO::Select, which has a sample code that does almost
exactly what I want. I modified it a bit and I have the following on the
server side:

$lsn = new IO::Socket::INET(Listen => 1, LocalPort => 9000);
$sel = new IO::Select ( $lsn );

while (@ready = $sel->can_read) {
 foreach $fh (@ready) {
  if ($fh == $lsn) {
   $new = $lsn->accept;
   $sel->add($new);
  } else {
   while (<$fh>) {
   print;
  }
  $sel->remove($fh);
  $fh->close;
  }
 }
}

This works perfectly for the first client, all the lines are received as
soon as you type them. But for a second client the lines are only displayed
when the first client quits. It probably has to do with it getting stuck on
that while <$fh> loop, but I can't see how to fix it. Seems like a very
simple problem but I can't get myself around it. How to I make two or more
clients have their lines printed simultaneously? Can someone point me in the
right direction?

Much appreciated,
~ Daniel.

Reply via email to