File: client.pl

package info (click to toggle)
collectl 3.7.4-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,624 kB
  • ctags: 119
  • sloc: perl: 14,928; sh: 429; makefile: 11
file content (62 lines) | stat: -rwxr-xr-x 1,148 bytes parent folder | download | duplicates (5)
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;
}