Leser: 1
|< 1 2 3 4 5 >| | 42 Einträge, 5 Seiten |
QuoteWarum muss eigendlich jeder sein eigenes kleines (in diesem Fall extrem schlechtes) Protokoll machen?
Es gibt doch schon IRC und Jabber!!!
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
67
68
#! /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);
}
}
}
}
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
use strict;
use warnings;
use IO::Socket::INET;
use IO::Select;
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 %users = ();
while(my @readable = $select->can_read) {
foreach my $socket (@readable) {
if($socket == $server) {
my ($client, $ip) = $socket->accept;
$select->add($client);
my ($port, $iaddr) = unpack_sockaddr_in($ip);
$iaddr = inet_ntoa($iaddr);
$users{rand(100)} = $iaddr;
print "New Client connected: $iaddr\n";
$client->print("Welcome\n");
} else {
my $line = $socket->getline;
if(defined $line) {
$line =~ s!\r?\n$!!g;
if(lc($line) eq "quit") {
print "Client quits\n";
delete_socket($select, $socket);
} else {
print "Client says: [$line]\n";
$socket->print("$line\n");
}
} else {
print "Connection closed by client\n";
delete_socket($select, $socket);
}
}
}
}
sub delete_socket {
my ($sel, $sock) = @_;
$sel->remove($sock);
$sock->close;
}
1
2
3
4
5
6
7
my ($client, $ip) = $socket->accept;
$select->add($client);
my ($port, $iaddr) = unpack_sockaddr_in($ip);
$iaddr = inet_ntoa($iaddr);
$users{rand(100)} = $iaddr;
print "New Client connected: $iaddr\n";
$client->print("Welcome\n");
|< 1 2 3 4 5 >| | 42 Einträge, 5 Seiten |