How can I share Perl data structures over a socket?

In sockets, I wrote a client server program. At first I tried to send a regular string, among which it sends a message. After that I tried to send the hash and array values ​​from client to server and server to client. When I print the values ​​with Dumper, it gives me only a control value. What to do to get the actual values ​​on the client server?

Server program:

use IO::Socket; use strict; use warnings; my %hash = ( "name" => "pavunkumar " , "age" => 20 ) ; my $new = \%hash ; #Turn on System variable for Buffering output $| = 1; # Creating aa new socket my $socket= IO::Socket::INET->new(LocalPort=>5000,Proto=>'tcp',Localhost => 'localhost','Listen' => 5 , 'Reuse' => 1 ); die "could not create $! \n" unless ( $socket ); print "\nUDPServer Waiting port 5000\n"; my $new_sock = $socket->accept(); my $host = $new_sock->peerhost(); while(<$new_sock>) { #my $line = <$new_sock>; print Dumper "$host $_"; print $new_sock $new . "\n"; } print "$host is closed \n" ; 

Client program

 use IO::Socket; use Data::Dumper ; use warnings ; use strict ; my %hash = ( "file" =>"log.txt" , size => "1000kb") ; my $ref = \%hash ; # This client for connecting the specified below address and port # INET function will create the socket file and establish the connection with # server my $port = shift || 5000 ; my $host = shift || 'localhost'; my $recv_data ; my $send_data; my $socket = new IO::Socket::INET ( PeerAddr => $host , PeerPort => $port , Proto => 'tcp', ) or die "Couldn't connect to Server\n"; while (1) { my $line = <stdin> ; print $socket $ref."\n"; if ( $line = <$socket> ) { print Dumper $line ; } else { print "Server is closed \n"; last ; } } 

I gave my sample program about what I am doing. Can someone tell me what I am doing wrong in this code? And what do I need to do to access the hash values?

+4
source share
2 answers

When you speak

 print $ref; 

you partially instruct Perl to convert $ref to a string (since only strings can be print ed). It turns out that links by default do not turn into very useful lines.

You need to turn $ref into a string that you can send over the wire and then decode on the other side to return the data. This process is called serialization. Data::Dumper output is actually a valid serialization of its arguments, but the main serialization module in Perl Storable .

Procedurally, you can say [1]

 use Storable qw(nfreeze); # nfreeze rather than freeze because # we want a network-portable string ... print nfreeze($ref); 

on the one hand and

 use Storable qw(thaw); ... my $ref = thaw($line); 

with another.

There is also an OO interface; read the Storable documentation for more information.

[1]: Pay attention to yaddayaddas. This is incomplete code that simply illustrates the key differences from your code.

+8
source

The problem is that you are sending links to the data, not the data itself. You need to serialize the data somehow. JSON is a very easy way to do this. There is also YAML .

+5
source

Source: https://habr.com/ru/post/1309621/


All Articles