#!/usr/bin/env perl
use strict;
use warnings;
use IO::Socket::INET qw(CRLF);
use IO::Select;
my $socket = IO::Socket::INET->new(
Listen => 5,
LocalPort => 7777,
Reuse => 1,
) or die $!;
my $reader = IO::Select->new($socket);
my @clients;
while (1) {
for my $ready ($reader->can_read()) {
if ($ready == $socket) {
my $client = $socket->accept();
$reader->add($client);
push @clients,$client;
} else {
my $buffer;
sysread($ready,$buffer,1024);
for my $client (@clients) {
if ($client != $ready) { # Dont echo to client whose chatt
+ing
syswrite($client,$buffer);
}
}
}
}
}
So I came up with this, it is pretty much all based on the same reference that karl mentioned to Network Programming by Stein. If you already have a group chat then it should be easy to adapt. instead of pushing your clients to an array as done here it would go into maybe a two dimensional array where each element is a client that talks to the other element and no one else. Or a hash with the key as the login name. You did not say who you want to talk to who. Maybe they enter a password and then that is the key to the hash that contains some structure that leads to the socket you want to write to. If that makes sense. |