I am trying to create a socket client in Perl. The server side is a C program running on the local host.
I have to send a hash over the socket. Here is the piece of code I am using.
sub applyGlobalConfig {
my ($globalConfig, $ignoreData) = @_;
my $socket = IO::Socket::INET->new(PeerAddr => $PEER_HOST,
PeerPort => $PEER_PORT,
Proto => "tcp",
Type => SOCK_STREAM)
or die "Couldn't connect to $PEER_HOST:$PEER_PORT : $@\n";
my $reconfigResult;
print $socket "$113\n";
close($socket);
unless ($reconfigResult) {
return 0;
}
return ERR_NULL;
}
Now, the question I have is, the $globalConfig will contain a hash reference, and I want to send this over a socket. I am unable figure out. I googled and found some reference to Dumper but couldn’t understand much. How do I send the hash over?
I’d prefer to use Storable module for this. Example:
Receive-side:
Send-side:
Tested by sending from i386 Linux to amd64 FreeBSD.
Also you can use Data::Dumper to make string from hash and then send over network but its dirty and buggy method.
UPD:
Try to use join/map combination:
Probably on C side its easier to use null-terminated string:
In this simple case i’d prefer to use last variant since its native to C.