Fredde87 has asked for the wisdom of the Perl Monks concerning the following question:
#!/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 every +body, I just received the following for one of you: $buf\r\n";} } else { # Client has disconnected $read_set->remove($rh); close($rh); } } } }
|
|---|