in reply to Passing hash reference over tcp client/server
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:
in your tcpclient.pl scriptprint "Received from Server :", %$data1, $/; # OR print "Received from Server :", Dumper($data1), $/;
#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();
Update: graff thanks for the eagle eye!#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();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Passing hash reference over tcp client/server
by graff (Chancellor) on Nov 12, 2012 at 23:51 UTC | |
|
Re^2: Passing hash reference over tcp client/server
by newbio (Beadle) on Nov 13, 2012 at 14:42 UTC | |
by graff (Chancellor) on Nov 14, 2012 at 11:22 UTC |