Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

perl : Passing hash , array through socket program betwen client and server

by pavunkumar (Scribe)
on May 13, 2010 at 04:20 UTC ( #839769=perlquestion: print w/ replies, xml ) Need Help??
pavunkumar has asked for the wisdom of the Perl Monks concerning the following question:

 
Hi All .

In sockets I have written the client server program .
First I tried to send the normal string among them it sends fine .
After that I am trying to send the hash and array values from client to server and server to client .
When I printing the values using Dumper . It is giving me only reference
. What Should I do for getting accessing the actual values in 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 a a 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 connecti +on 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 have given my sample program about what I am doing ,
Can any one tell me what I am doing wrong in this code.
And what I need to do for accessing the hash values .

Comment on perl : Passing hash , array through socket program betwen client and server
Select or Download Code
Re: perl : Passing hash , array through socket program betwen client and server
by nagalenoj (Friar) on May 13, 2010 at 05:21 UTC
Re: perl : Passing hash , array through socket program betwen client and server
by NetWallah (Abbot) on May 13, 2010 at 05:24 UTC
    You can wrap the socket send/receive around appropriate parts of the following functional snippet:
    #$perl -w use Data::Dumper ; use warnings ; use strict ; my %hash = ( "file" =>"log.txt" , size => "1000kb") ; $Data::Dumper::Deepcopy =1; my $send_string = Dumper \%hash; # Pretend that $send_string has been sent across the socket, then recv +d. # Since $sendstring starts with $VAR1={...}, for "strict", we need # to pre-pend the "my", such that it reads "my $VAR1={...}" my $rcv_string = "my " . $send_string; my %hash_rehydrated = %{ +eval $rcv_string }; # Assuming trust here... print Dumper \%hash_rehydrated;

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Re: perl : Passing hash , array through socket program betwen client and server
by BrowserUk (Pope) on May 13, 2010 at 06:20 UTC

    A couple of things:

    1. You'd almost certainly be better to use Storable freeze/thaw to serialise data for transmission via a socket.

      It packs and unpacks faster; is more compact and therefore transmits faster; and doesn't rely upon eval for unpacking and is therefore safer.

    2. A proto 'tcp' listening port combined with a 'tcp' client connect, does not make for a "UDP server".
      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 $socket = new IO::Socket::INET ( PeerAddr => $host , PeerPort => $port , Proto => 'tcp', )

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-09-21 16:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (172 votes), past polls