Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Passing hash reference over tcp client/server

by newbio (Beadle)
on Nov 12, 2012 at 22:32 UTC ( #1003516=perlquestion: print w/ replies, xml ) Need Help??
newbio has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am trying to pass a hash reference from server to client. I then try to print the content of the hash. However, there appears some error in my programs and the client does not print the output and simply hangs on its terminal.

Please let me know what is a good way to do this.

Thank you.

#tcpserver.pl #!/usr/bin/perl use strict; use warnings; use IO::Socket::INET; use Storable qw(nfreeze); $| = 1; my ($socket,$clientsocket); my ($peeraddress,$peerport); my $data; my $line; my $inputfile = "list.txt"; my %names=(); my $reftohash; open(RF, "$inputfile"); # read file while ($line=<RF>) { chomp $line; $names{$line}=1; } #set hash reference $reftohash=\%names; $socket = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => '5000', Proto => 'tcp', Listen => 5, Reuse => 1 ) or die "ERROR in Socket Creation : $!\n"; while(1) { $clientsocket = $socket->accept(); print $clientsocket nfreeze($reftohash); } $socket->close(); ########## #tcpclient.pl #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use IO::Socket::INET; use Storable qw(thaw); $| = 1; my $socket; my $data; my $data1; $socket = new IO::Socket::INET ( PeerHost => 'localhost', PeerPort => '5000', Proto => 'tcp', ) or die "ERROR in Socket Creation : $!\n"; $data = <$socket>; $data1 = thaw($data); print "Received from Server : $data1\n"; foreach my $i (keys %{$data1}) { print "$i=>$data1->{$i}\n"; } $socket->close();

Comment on Passing hash reference over tcp client/server
Download Code
Re: Passing hash reference over tcp client/server
by 2teez (Priest) on Nov 12, 2012 at 23:47 UTC

    Hi newbio,

    I then try to print the content of the hash

    To do that,
    change this:

    print "Received from Server : $data1\n";

    to this:
    print "Received from Server :", %$data1, $/; # OR print "Received from Server :", Dumper($data1), $/;
    in your tcpclient.pl script

    However, there appears some error in my programs
    What are the errors?

    and the client does not print the output and simply hangs on its terminal.
    The terminal didn't hang, from the server.pl, remove the endless while loop, you should have your output.

    Below: please check the changes made to the OP scripts:
    #tcpserver.pl #!/usr/bin/perl use strict; use warnings; use IO::Socket::INET; use Storable qw(nfreeze); $| = 1; my ( $socket, $clientsocket ); my ( $peeraddress, $peerport ); my $data; my $line; my $inputfile = "list.txt"; my %names = (); my $reftohash; open my $fh, '<', $inputfile or die "can't open file: $!"; # read f +ile while ( $line = <$fh> ) { chomp $line; $names{$line} = 1; } close $fh or die "can't close file: $!"; #set hash reference $reftohash = \%names; $socket = new IO::Socket::INET( LocalHost => 'localhost', LocalPort => '5000', Proto => 'tcp', Listen => 5, Reuse => 1 ) or die "ERROR in Socket Creation : $!\n"; #while(1) { $clientsocket = $socket->accept(); print $clientsocket nfreeze($reftohash); #} $socket->close();
    #tcpclient.pl #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use IO::Socket::INET; use Storable qw(thaw); $| = 1; my $socket; my $data; my $data1; $socket = new IO::Socket::INET( PeerHost => 'localhost', PeerPort => '5000', Proto => 'tcp', ) or die "ERROR in Socket Creation : $!\n"; $data = <$socket>; $data1 = thaw($data); print "Received from Server :", Dumper($data1), $/; # OR %$data1 foreach my $i ( keys %{$data1} ) { print "$i=>$data1->{$i}\n"; } $socket->close();
    Update: graff thanks for the eagle eye!

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      Actually, since the OP is using Data::Dumper, it would be better to change to this:
      print "Received from Server :\n", Dumper( $data1 );
      Thank you very much 2teez for your suggestion. When I run the client, the program now produces the desired results. However, the problem now is that upon successful client execution, the server program also exits. What I need is that the server program remains persistent in memory and keeps serving multiple client requests that it might receive. Any suggestion? Thanks again!
        Um, if you're using the served code snippet as posted by 2teez, did you happen to uncomment these lines?
        #while (1) { ... #}

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003516]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2014-09-02 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls