Thread "HASH"-Socket lesen (12 answers)
Opened by Oliver at 2019-05-27 22:11

Gast Oliver
 2019-05-27 22:11
#190043 #190043
Hallo
Ich habe da ein - ich glaube - Denkproblem......

Folgender Aufbau:
In einem Array befinden sich Hashes. In den Hashes werden Daten zu jeweils einem Netzwerksocket gespeichert:

Code: (dl )
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 => ""
}
);


Dann werden sozusagen die einzelnen Sockets (hier auf nur zwei reduziert) nacheinander abgefragt. Natürlich mit ein wenig Fehlerhandling und Logging:

Code: (dl )
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'} -> $!");
}
}


Mein Problem ist, dass die Zeile 33

$Response=<$NetDevice[$NetDeviceID]{"Socket"}>;

NICHT die Antwort in $Response speichert sondern eine Zeigeradresse (???)
Das sieht dann ungefähr so aus:

IO::Socket::INET__with__IO::Socket::Timeout::Role::SetSockOpt=GLOB(0x5619a318e300)

Ich komme nicht dahinter, wo ich was flasch "übergebe".
Kann mir jemand einen Tipp geben?

Danke, Grüße
Oliver
Last edited: 2019-05-28 09:34:48 +0200 (CEST)

View full thread "HASH"-Socket lesen