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

I'm trying to write a client and server using sockets (using ActivePerl under Win32). I need to allow multiple clients to connect to a server. I need to have the server send info to the clients (server writes and client reads) *BUT* I also need to allow the clients to write to the server (client writes and server reads). The "writes" will be non-interactive--I need to stay away from STDIN! Most of the code I've seen has an infinite loop for reading from the client, but that won't work here. I'm guessing that an OO design would work the best, as in: sub onClientConnect { } sub onClientRead { } sub onClientWrite { } sub onClientDisconnect { } but this may be too difficult to do. Does anyone have any ideas...or better yet, code examples? Thanks! Jeremy
  • Comment on Non-interactive Client/Server Using Sockets

Replies are listed 'Best First'.
Re: Non-interactive Client/Server Using Sockets
by Anonymous Monk on Sep 07, 2003 at 20:04 UTC
    This "problem" has been solved numerous times. Please consult CPAN.
      my $server = new IO::Socket::INET(); while(my $client = $server->accept()) { next if fork; my $line = <$client>; print $client "You said: $line\m"; exit; }
      The server goes in to an infinte loop accepting new client connections, when it receives a connection it creates a new $client containg that connection and the forks. The parent goes back to waiting for the connection while the child reads a line from the client and prints one back to it. You could use various ->read() methods, but I find it much simpler to just read based on new lines.