Leser: 3
|< 1 2 3 4 >| | 33 Einträge, 4 Seiten |
1
2
3
4
5
6
while(my $client = $server->accept()) {
$select->add($client);
foreach my $socket ($select->can_read(5)) {
print "$socket\n";
}
}
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
use strict;
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);
while(my @readable = $select->can_read) {
foreach my $socket (@readable) {
if($socket == $server) {
# Create a new socket
print "New client connects\n";
my $client = $socket->accept; # kehrt direkt zurück
$select->add($client);
} 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";
}
} 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
8
9
10
11
12
13
14
use IO::Socket;
my $server = IO::Socket::INET->new(
LocalPort => 2005,
type => SOCK_STREAM,
Reuse => 1,
Listen => 10
)
or die "Server läuft nicht: $@\n";
while (my $client = $server->accept()) {
print "Anfrage\n";
}
close($server);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
use IO::Socket;
my $server = IO::Socket::INET->new(
LocalPort => 2005,
type => SOCK_STREAM,
Reuse => 1,
Listen => 10
)
or die "Server läuft nicht: $@\n";
my ($data_read, $flags);
$server->recv($data_read, $flags)
or die "Kann nicht empfangen: $!";
|< 1 2 3 4 >| | 33 Einträge, 4 Seiten |