Re: Sending HASH over TCP

From: Chris (ceo_at_nospan.on.net)
Date: 02/14/04

  • Next message: Daniel M. Drucker: "Re: Term::Prompt broken? Or am I misreading documentation?"
    Date: Sat, 14 Feb 2004 07:53:15 GMT
    
    

    Brad wrote:
    > I have been working on this for a few days now, and still feel stuck.
    > I have a large HASH which I need to send via TCP to a server. I need
    > to use IO::Socket::INET for the TCP connection. While there is
    > probably a _better_ way, I have been playing around with Data::Dumper
    > convert the HASH, then rebuild it on the other end. The problem is I
    > cannot seem to rebuild the data.
    >
    > Code:
    >
    > Client (sending HASH)
    > ---------------------
    > use Data::Dumper;
    > use IO::Socket::INET;
    >
    > $TEST{'test'}{'test1'} = 1;
    > $TEST{'test2'}{'test3'} = 2;
    >
    > $socket = IO::Socket::INET->new(PeerAddr => localhost,
    > PeerPort => 2002,
    > Proto => "tcp",
    > Type => SOCK_STREAM)
    > or die "Couldn't connect to $remote_host:$remote_port : $!\n";
    >
    > $Data::Dumper::Pair = " : "; # specify hash key/value
    > separator
    > $Data::Dumper::Indent = 0;
    > # send something over the socket,
    > print $socket Dumper(\%TEST);
    >
    > # and terminate the connection when we're done.
    > close($socket);
    > ---------------------
    >
    > Server
    > ---------------------
    > use Data::Dumper;
    > use IO::Socket::INET;
    >
    > $server = IO::Socket::INET->new(LocalPort => 2002,
    > Type => SOCK_STREAM,
    > Reuse => 1,
    > Listen => 10 ) # or SOMAXCONN
    > or die "Couldn't be a tcp server on port $server_port: $!\n";
    >
    > while ($client = $server->accept()) {
    > $data = <$client>;
    > #print "$data\n";
    > #$Data::Dumper::Pair = " : "; # specify hash key/value
    > separator
    > #$Data::Dumper::Indent = 0;
    > #eval $data;
    > print Dumper $data;
    > }
    >
    > close($server);
    > ---------------------
    >
    > I have tried other methods of re-building the hash on the server-end.
    > The answer is probably right in front of me, but I just don't see it
    > on the module's page (
    > http://search.cpan.org/~ilyam/Data-Dumper-2.121/Dumper.pm ) or in the
    > perldoc.
    >
    > Any help, or any ideas for a better solution, is appreciated.
    >

    Aside from Ben Morrow's solution (which I wish I had the time to try),
    if you are stuck on a "Dumper()" solution, you might get more mileage
    out of the XML::Dumper::pl2xml() and xml2pl() routines. Write yourself
    a psuedo-web service that passes XML structures back and forth.
    pl2xml() and xml2pl() will handle this rightly.

    Without digging into your code too much (seeing as it's way past my bed
    time) and surmising WHY you might be trying to accomplish this in the
    first place, you may even have your wants satisfied by tossing the
    structure passing altogether and writing a real web service using
    XML-RPC. Check out Frontier::RPC2 for more information. XML-RPC in
    Perl is painless to setup and quite powerful (though inherently insecure
    I have to admit). You can setup XML-RPC on your port 2002 if you want.

    Chris
    -----
    Chris Olive
    chris 0at0 00-00-00 technologEase 0dot0 com
    http://www.technologEase.com
    (pronounced "technologies")


  • Next message: Daniel M. Drucker: "Re: Term::Prompt broken? Or am I misreading documentation?"

    Relevant Pages

    • Re: Encryption -- Blowfish limited to 8 byte passowrds?
      ... Chris wrote: ... Why won't Java allow other length ... > some kind of hash function. ... > algorithm itself is only acting on an 8-byte key. ...
      (comp.lang.java)
    • Re: Efficient Text File Copy
      ... >> Why do I recommend Chris Torek's hash? ... I have the obvious testbed in hashlib, ... verification suite include the awful hash function 1. ...
      (alt.comp.lang.learn.c-cpp)
    • Re: Efficient Text File Copy
      ... >> Why do I recommend Chris Torek's hash? ... I have the obvious testbed in hashlib, ... verification suite include the awful hash function 1. ...
      (comp.lang.c)
    • Re: ISO vanilla class
      ... > Personally I'd use a hash not a hash reference: ... > both required and supposed to be a scalar. ... I wrote my own base class module that does most of what ... Chris Olive ...
      (comp.lang.perl.misc)
    • Re: Processing multiple delimiter files
      ... You got stuck way before here (where you claim that you can ... Here is one way to create the hash: ... use warnings; ... use strict; ...
      (comp.lang.perl.misc)