-
Notifications
You must be signed in to change notification settings - Fork 17
/
echo-server.pl
50 lines (29 loc) · 908 Bytes
/
echo-server.pl
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
use warnings; use strict;
use IO::Socket::INET;
use threads;
my $port_listen = 2808;
$| = 1; # Autoflush
my $socket = IO::Socket::INET->new(
LocalHost => '0.0.0.0',
LocalPort => $port_listen,
Proto => 'tcp',
Listen => 5,
Reuse => 1
) or die "Cannot create socket";
print "Waiting for tcp connect to connet on port $port_listen (see echo-client.pl)\n";
while (1) {
my $client_socket = $socket->accept();
my $client_address = $client_socket->peerhost;
my $client_port = $client_socket->peerport;
print "$client_address $client_port has connected\n";
threads -> create(\&connection, $client_socket);
}
$socket -> close;
sub connection {
my $client_socket = shift;
while (1) {
my $data = <$client_socket>;
return if $data eq "";
print $client_socket "Ok $data";
}
}