Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: ONE-to-MANY Socket-Server

by l2kashe (Deacon)
on Jul 22, 2003 at 19:30 UTC ( #276891=note: print w/replies, xml ) Need Help??

in reply to ONE-to-MANY Socket-Server

Personally I have gotten alot of mileage out of the following code. It is from the poe cookbook. POE is a amazing little (well not so little) series of modules which provide, quasi multithreaded behaviour, without the threads. Here is a simple chat server. Each new connection will be assigned a numeric ID. When someone inputs text, everyone else sees the message as coming from the originating ID. POE takes a little to get used to, but with this as a base I have managed to quickly add some basic commands, the concepts of rooms and player nicks.

#!/usr/bin/perl # This program is a simple chat server. It allows multiple people to # connect and exchange messages. It's a very simple example, but it # can be the basis of many multiuser things. use warnings; use strict; use POE; use POE::Component::Server::TCP; # Create the server on port 32080, and start it running. POE::Component::Server::TCP->new ( Alias => "chat_server", Port => 32080, InlineStates => { send => \&handle_send }, ClientConnected => \&client_connected, ClientError => \&client_error, ClientDisconnected => \&client_disconnected, ClientInput => \&client_input, ); $poe_kernel->run(); exit 0; # This is a plain Perl function (not a POE event handler) that # broadcasts a message to all the users in the chat room. The %users # hash is used to track connected people. my %users; sub broadcast { my ( $sender, $message ) = @_; foreach my $user ( keys %users ) { if ( $user == $sender ) { $poe_kernel->post( $user => send => "You $message" ); } else { $poe_kernel->post( $user => send => "$sender $message" ); } } } # Handle an outgoing message by sending it to the client. sub handle_send { my ( $heap, $message ) = @_[ HEAP, ARG0 ]; $heap->{client}->put($message); } # Handle a connection. Register the new user, and broadcast a message # to whoever is already connected. sub client_connected { my $session_id = $_[SESSION]->ID; $users{$session_id} = 1; broadcast( $session_id, "connected." ); } # The client disconnected. Remove them from the chat room and # broadcast a message to whoever is left. sub client_disconnected { my $session_id = $_[SESSION]->ID; delete $users{$session_id}; broadcast( $session_id, "disconnected." ); } # The client socket has had an error. Remove them from the chat room # and broadcast a message to whoever is left. sub client_error { my $session_id = $_[SESSION]->ID; delete $users{$session_id}; broadcast( $session_id, "disconnected." ); $_[KERNEL]->yield("shutdown"); } # Broadcast client input to everyone in the chat room. sub client_input { my ( $kernel, $session, $input ) = @_[ KERNEL, SESSION, ARG0 ]; my $session_id = $session->ID; broadcast( $session_id, "said: $input" ); }

use perl;

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://276891]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2022-08-09 19:29 GMT
Find Nodes?
    Voting Booth?

    No recent polls found