Re: Sending HASH over TCP
From: Chris (ceo_at_nospan.on.net)
Date: 02/14/04
- Previous message: Chris: "Re: configure, WIN32"
- In reply to: Brad: "Sending HASH over TCP"
- Next in thread: Mina Naguib: "Re: Sending HASH over TCP"
- Reply: Mina Naguib: "Re: Sending HASH over TCP"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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")
- Previous message: Chris: "Re: configure, WIN32"
- In reply to: Brad: "Sending HASH over TCP"
- Next in thread: Mina Naguib: "Re: Sending HASH over TCP"
- Reply: Mina Naguib: "Re: Sending HASH over TCP"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|