blob: 0f0f2d9a0ff5fdbfda8781f942eac6ed8824634e (
plain)
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
|
#!/usr/bin/perl
use IO::Socket;
use IO::Select;
# Create a socket to listen on.
#
my $listener =
IO::Socket::INET->new( LocalPort => 8008, Listen => 5, Reuse => 1 );
die "Can't create socket for listening: $!" unless $listener;
print "Listening for connections on port 8008\n";
my $readable = IO::Select->new; # Create a new IO::Select object
$readable->add($listener); # Add the listener to it
while(1) {
# Get a list of sockets that are ready to talk to us.
#
my ($ready) = IO::Select->select($readable, undef, undef, undef);
foreach my $s (@$ready) {
# Is it a new connection?
#
if($s == $listener) {
# Accept the connection and add it to our readable list.
#
my $new_sock = $listener->accept;
$readable->add($new_sock) if $new_sock;
print $new_sock "Welcome!\r\n";
print "connection :-o\n\n";
} else { # It's an established connection
my $buf = <$s>; # Try to read a line
# Was there anyone on the other end?
#
if( defined $buf ) {
# If they said goodbye, close the socket. If not,
# echo what they said to us.
#
if ($buf =~ /(good)?bye/i) {
print $s "See you later!\n";
$readable->remove($s);
$s->close;
} else {
print $s "You said: $buf\n";
print "$buf\n";
}
} else { # The client disconnected.
$readable->remove($s);
$s->close;
print STDERR "Client Connection closed\n";
}
}
}
}
|