#!/usr/bin/perl
# Serveur.
$| = 1;
use IO::Socket;
use IO::Select;
use Sys::Hostname;
use Term::ANSIColor;
#
http://perldoc.perl.org/Term/ANSIColor.html
#
http://perldoc.perl.org/IO/Select.html
# Creation du socket.
my $myself = hostname();
my $listening_addr = '10.9.178.25';
my $listening_port = 3142;
$main_socket = IO::Socket::INET->new(
LocalHost => $listening_addr,
LocalPort => $listening_port,
Proto => 'tcp',
Type => SOCK_STREAM,
Reuse => 1,
Listen => 5) or die 'Unable to listen on socket : '.$!."\n";
print 'Server started on : '.$listening_addr.':'.$listening_port."\n";
print 'waiting for clients...'."\n";
$main_socket->autoflush(1);
$readable_handles = new IO::Select();
$readable_handles->add($main_socket);
while (1) { #Infinite loop
# select() blocks until a socket is ready to be read or written
($new_readable) = IO::Select->select($readable_handles, undef, undef, 0);
# If it comes here, there is at least one handle
# to read from or write to. For the moment, worry only about
# the read side.
foreach $sock (@$new_readable) {
$sock->autoflush(1);
if ($sock == $main_socket) {
$new_sock = $sock->accept();
print $new_sock 'msg from server : connection established !';
# Add it to the list, and go back to select because the
# new socket may not be readable yet.
$name = gethostbyaddr($new_sock->peeraddr, AF_INET);
$peer_addr = $new_sock->peerhost();
$peer_port = $new_sock->peerport();
print 'new client : '.$name.'@'.$peer_addr.':'.$peer_port."\n";
$readable_handles->add($new_sock);
} else {
# It is an ordinary client socket, ready for reading.
$sock->autoflush(1);
$pid = fork;
die 'unable to fork client !' unless defined ($pid);
if ($pid ==0) {
# Le fils recoit les donnees du client.
$buf = <$sock>;
if ($buf) {
$name = gethostbyaddr($sock->peeraddr, AF_INET);
$peer_addr = $sock->peerhost();
$peer_port = $sock->peerport();
if ( $name eq 'mschapi-bb' ) {
print color("MAGENTA"),'msg from : '.$name.'@'.$peer_addr.':'.$peer_port.' : '.$buf,color("reset");
} elsif ( $name eq 'mschapo-bb' ) {
print color("CYAN") ,'msg from : '.$name.'@'.$peer_addr.':'.$peer_port.' : '.$buf,color("reset");
} else {
print 'msg from : '.$name.'@'.$peer_addr.':'.$peer_port.' : '.$buf;
}
# print $sock 'koukou';
# print $sock ' connection established !';
} else {
# Client closed socket. We do the same here, and remove
# it from the readable_handles list
$name = gethostbyaddr($sock->peeraddr, AF_INET);
$peer_addr = $sock->peerhost();
$peer_port = $sock->peerport();
# Attention boucle infinie possible ici...
print 'client has gone away : '.$name.'@'.$peer_addr.':'.$peer_port."\n";
$readable_handles->remove($sock);
close($sock);
}
} else {
# Le pere envoie les donnees au client.
while ($ligne = <>) {
if ($ligne) {
foreach $sock (@$new_readable) {
$sock->autoflush(1);
print $sock color("red"),$ligne,color("reset");
# print $sock "\a".$ligne;
# print $sock $ligne;
}
}
}
# ca marche ?
#$readable_handles->remove($sock);
#close($sock);
}
}
}
}
$sock->close();
$main_socket->close();
Partager