1
2
3
4
5
6
7
8
9
10
11
12
my @NetDevice =(
{ Name => "EG0",
IP => "192.168.0.111",
Socket => undef,
LastError => ""
},
{ Name => "XPK",
IP => "192.168.0.119",
Socket => undef,
LastError => ""
}
);
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
sub send_singlecast {
my ($NetDeviceID, $Send) = @_;
my $Response;
# Check if already connected
if (! defined $NetDevice[$NetDeviceID]{"Socket"}) {
if ($Debug eq "YES") { print "Try to reconnect to Server $NetDevice[$NetDeviceID]{'Name'} ($NetDevice[$NetDeviceID]{'IP'})..."}
$NetDevice[$NetDeviceID]{"Socket"} = new IO::Socket::INET (
Broadcast => 0,
PeerHost => $NetDevice[$NetDeviceID]{"IP"},
PeerPort => '5000',
Proto => 'tcp',
Timeout => 2,
);
if (defined $NetDevice[$NetDeviceID]{"Socket"}) {
if ($Debug eq "YES") { print "success.\n"; }
$NetDevice[$NetDeviceID]{"Socket"}->autoflush(1);
if ($Debug eq "YES") { print "Enable timeouts.\n"; }
IO::Socket::Timeout->enable_timeouts_on($NetDevice[$NetDeviceID]{"Socket"});
$NetDevice[$NetDeviceID]{"Socket"}->read_timeout(2);
$NetDevice[$NetDeviceID]{"Socket"}->write_timeout(2);
}
}
if (defined $NetDevice[$NetDeviceID]{"Socket"}) {
# Asking for status or sending command
$NetDevice[$NetDeviceID]{"Socket"}->send($Send);
if ($Debug eq "YES") { print "Command '$Send' send to $NetDevice[$NetDeviceID]{'Name'}, waiting for response..."; }
# Read the socket data sent by server
$Response=<$NetDevice[$NetDeviceID]{"Socket"}>;
if (! $Response && ( 0+$! == ETIMEDOUT || 0+$! == EWOULDBLOCK )) {
if ($Debug eq "YES") { print "Timeout by reading from socket!\n"; }
write_errorlog("ERROR by reading from Socket : $NetDevice[$NetDeviceID]{'Name'} -> Timeout");
return("ERROR by reading from Socket : $NetDevice[$NetDeviceID]{'Name'} -> Timeout");
} else {
if ($Debug eq "YES") { print "received from Server : $Response\n"; }
return($Response);
}
} else {
if ($Debug eq "YES") { print "ERROR in Socket Creation : $!\n"; }
# Write ErrorLog
write_errorlog("ERROR in Socket Creation : <$NetDevice[$NetDeviceID]{'Name'} -> $!");
return("ERROR in Socket Creation : <$NetDevice[$NetDeviceID]{'Name'} -> $!");
}
}
1 2 3 4 5 6 7 8 9 10 11
{ use Data::Dumper; local $Data::Dumper::Purity = 1; my $sock = $NetDevice[$NetDeviceID]{"Socket"}; print "DEBUG\n"; print "--------------------------------------\n"; print Dumper($sock); $Response=<$sock>; print Dumper($Response); print "--------------------------------------\n"; }
1 2 3 4
use bytes; while( read( $socket, my $buffer, $bufflen )){ $res .= $buffer; }
IO::Socket::INET__with__IO::Socket::Timeout::Role::SetSockOpt=GLOB(0x5619a318e300)
$Response = $NetDevice[$NetDeviceID]{"Socket"} -> getline;