Leser: 22
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#!/usr/bin/perl $port=65000; $linelength=1000; require 5.002; use IO::Socket; use IO::Select; &initialize; while(@ready = $select->can_read) { for $socket (@ready) { if($socket == $listen) { &connection; } else { $socket->recv($line,$linelength); if($line eq "") { &disconnection; } else { &broadcast($line); } } } } sub initialize { $ARGV[0] ? $port=$ARGV[0] : $port=$port; $nullstring="\000"; $listen = IO::Socket::INET->new(Proto => "tcp", LocalPort => $port, Listen => 1, Reuse => 1) or die $!; $select = IO::Select->new($listen); } sub connection { $new = $listen->accept; $select->add($new); print $new->fileno . ": connected\n"; &numclients; } sub disconnection { print $socket->fileno . ": disconnected\n"; $select->remove($socket); $socket->close; &numclients; } sub numclients { $numofclients=$select->count()-1; $clientstext="<NUMCLIENTS>$numofclients</NUMCLIENTS>"; &broadcast($clientstext.$nullstring); } sub broadcast { for $eachsocket ($select->handles) { if ($eachsocket==$listen) { next; } else { $eachsocket->send($_[0]) or do { &disconnection; } } } } 1;
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 46 47 48 49 50 51 52 53 54 55 56 57 58
#!/usr/bin/perl #use strict; use IO::Socket; use constant MYPORT => 2000; my $sock = ''; my $client = ''; $sock = new IO::Socket::INET(LocalPort => MYPORT, Reuse => 1, Listen => 5) or die "can't create local socket: $@\n"; # Zombies verhindern $SIG{'CHLD'} = sub { wait; }; my $PaPID; my $ChPID; print "Accepting connections on Port ", MYPORT, "...\n"; while(defined($sock)){ #while ($client = $sock->accept()) #{ # Verbindung ist aufgebaut close $new_sock; my $new_sock = $sock->accept() or next; next if fork != 0; #print "Accepted connection from ", #$client->peerhost(), ":", $client->peerport(), "\n"; my $text; $SIG{CHLD} = sub { wait; }; $PaPID = $$; $ChPID = fork(); if($ChPID == 0){ while (<$new_sock>) { if($_ eq "help\r\n"){ print $new_sock "wuuuussaaa\n"; }elsif($_ eq "xx\r\n"){ print $new_sock "xx? what a crap ...\n"; }else{ print $new_sock "unknow command -> "; print $new_sock $_, "\n"; } } kill 15,$PaPID; exit; } } close $sock;