-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.pl
executable file
·62 lines (54 loc) · 1.12 KB
/
client.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
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/perl -w
use IO::Socket;
use IO::Select;
if (!defined($ARGV[0]))
{
print "usage: client.pl address[:port]\n";
exit;
}
($address,$port)=split(/:/, $ARGV[0]);
$port=2655 if !defined($port);
$SIG{"INT"}=\&sigInt; # for ^C
select STDOUT;
$|=1;
while (1)
{
$socket=new IO::Socket::INET(
PeerAddr => $address,
PeerPort => $port,
Proto => 'tcp',
Timeout =>1);
if (!defined($socket))
{
print "Couldn't connect to server, retrying...\n";
sleep 1;
next;
}
print "Socket opened on $address:$port\n";
$select = new IO::Select($socket);
while ($socket ne '')
{
$buffer='';
while (my @ready=$select->can_read(10))
{
$bytes=sysread($socket, $line, 100);
#print "BYTES: $bytes\n";
if ($bytes==0)
{
print "Socket closed on other end\n";
$socket='';
last;
}
$buffer.=$line;
@handles=($select->can_read(0));
last if scalar(@handles)==0;
}
print "$buffer" if $buffer ne '';
}
}
sub sigInt
{
print "Close Socket\n";
$socket->close() if defined($socket) && $socket ne '';
exit;
}