Advertisement
Advertisement
| 03.11.2008 at 06:39AM PDT, ID: 23231624 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: |
#!/usr/bin/perl -w
#
# Socket for communication
# Libraries
use IO::Socket;
use IO::Select;
use IO::Handle;
my @sockets;
my $s = new IO::Socket::INET (
LocalHost => '192.168.0.1',
LocalPort => '1234',
Proto => 'tcp',
Listen => 16,
Reuse => 1,
);
die "Could not create socket: $!\n" unless $s;
$read_set = new IO::Select(); # create handle set for reading
$read_set->add($s); # add the main socket to the set
while (1) {
my ($rh_set) = IO::Select->select($read_set, undef, undef, 0);
foreach $rh (@$rh_set) {
if ($rh == $s) {
# Client has connected
$ns = $rh->accept();
$read_set->add($ns);
} else {
$buf = <$rh>;
if($buf) {
# Client has sent something
$buf =~ s/\n|\r//g;
my @sockets = $read_set->can_write();
foreach my $sck(@sockets){print $sck "Hi everybody, I just received the following for one of you: $buf\r\n";}
}
else {
# Client has disconnected
$read_set->remove($rh);
close($rh);
}
}
}
}
|