#! /usr/bin/perl use strict; use warnings; use IO::Socket::INET; use IO::Select; use Data::Dumper; # Listensocket erstellen my $server = IO::Socket::INET->new(    LocalPort => 2005,    type => SOCK_STREAM,    Reuse => 1,    Listen => 10 ) or die "Server läuft nicht: $@\n"; my $select = IO::Select->new($server); my %hash; # warten, bis ein client sich anmeldet, oder # man von bereits angemeldeten clients lesen kann my $answer = 0; while(my @readable = $select->can_read) {   foreach my $socket (@readable) {      if($socket == $server) { # wenn der socket gleich dem listensocket ist         # Create a new socket         print "New client connects\n";         my $client = $socket->accept; # kehrt direkt zurück         $select->add($client); # in select object aufnehmen         my $name = $client->getline; # von socket lesen         if(defined $name) { # kein Fehler?            $name =~ s!\r?\n$!!g; # Zeilenumbruch entfernen            my ($rname) = $name =~ /^name: ([^\s]+)$/;            print "rname: ",$rname,"\n";            $hash{$_}->print($rname." has connected\n") for(keys(%hash));            $hash{$rname} = $client;         } else {           next;         }      } else { # sonst         my $line = $socket->getline; # von socket lesen         if(defined $line) { # kein Fehler?            $line =~ s!\r?\n$!!g; # Zeilenumbruch entfernen            if(lc($line) eq "quit") { # falls der Client beenden will               print "Client quits\n";               delete_socket($select, $socket);            }            elsif($line =~ /^msg\s([^\s]+)\s*(.*)$/){              my %reverse = reverse %hash;              print "name: ",$1,"\n";              my $socket_tmp = $hash{$1};              if($1 eq 'all'){                $hash{$_}->print($reverse{$socket}.': '.$2."\n") for(keys(%hash));                next;              }              $socket_tmp->print($reverse{$socket}.': '.$2."\n");            }            else { # ansonsten, message ausgeben               print "Client says: [$line]\n";            }         } else {           print "Connection closed by client\n";           delete_socket($select, $socket);         }      }   } }