forked from dbratcher/MediaKhan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.pl
41 lines (35 loc) · 1.08 KB
/
pong.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
use Time::HiRes qw(gettimeofday);
use IO::Socket::INET;
# auto-flush on socket
$| = 1;
# creating a listening socket
my $socket = new IO::Socket::INET (
LocalHost => '128.61.26.47',
LocalPort => '7777',
Proto => 'tcp',
Listen => 5,
Reuse => 1
);
die "cannot create socket $!\n" unless $socket;
print "server waiting for client connection on port 7777\n";
while(1)
{
# waiting for a new client connection
my $client_socket = $socket->accept();
# get information about a newly connected client
my $client_address = $client_socket->peerhost();
my $client_port = $client_socket->peerport();
print "connection from $client_address:$client_port\n";
# read up to 1024 characters from the connected client
my $data = "";
while($data ne "end") {
$client_socket->recv($data, 1024);
#print "data: $data\n";
($seconds, $micro) = gettimeofday;
$time2 = $seconds + $micro/1000000.0;
$client_socket->send($time2);
# notify client that response has been sent
}
shutdown($client_socket, 1);
}
$socket->close();