-
Notifications
You must be signed in to change notification settings - Fork 0
/
pl_sock_stcp02.pl
executable file
·32 lines (32 loc) · 1.11 KB
/
pl_sock_stcp02.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
#!/usr/bin/perl -w
use strict;
use IO::Socket qw (:DEFAULT :crlf);
use constant MY_ECHO_PORT => 2007;
$/=CRLF;
my ($bytes_out,$bytes_in)= (0,0);
my $quit =0;
$SIG{INT} =sub { $quit++};
my $port =shift || MY_ECHO_PORT;
my $sock =IO::Socket::INET->new ( Listen => 20,
LocalPort =>$port,
Timeout =>60*60,
Reuse => 1)
or die "Can't create listening socket: $!\n";
warn "waiting for incomeng connectins on port $port....\n";
while (!$quit) {
next unless my $session = $sock->accept;
my $peer =gethostbyaddr($session -> peeraddr,AF_INET) || $session -> peerhost;
my $port = $session -> peerport ;
warn "Connection from [$peer,$port]\n";
while (<$session>) {
$bytes_in +=length($_);
chomp;
my $msg_out =(scalar reverse $_) . CRLF;
print $session $msg_out;
$bytes_out +=length($msg_out);
}
warn "connection from [$peer,$port] finished\n";
close $session;
}
print STDERR "bytes_sent =$bytes_out,bytes_received= $bytes_in \n";
close $sock;