All you need to do to turn "echo" into a chat server is send messages to
everyone, not just yourself. We can use $select, which returns all the
sockets we gave to $select using new() or add(), to
find out who "everyone" is. So we make these changes:
$socket->recv($line,80);
if($line eq "") {
print $socket->fileno . ": disconnected\n";
$select->remove($socket);
$socket->close;
};
my $socket;
# broadcast to everyone. Close connections where send() fails.
for $socket ($select->handles) {
next if($socket==$listen);
$socket->send($line) or do {
print $socket->fileno . ": disconnected\n";
$select->remove($socket);
$socket->close;
};
}
Here's the full source for chat.
next page»