Professional Web Applications Themes

Best way to Send Hash to a Socket - PERL Beginners

Hello list, To day I am trying to figure out the best way to send a hash to a socket and have the receiver be able to use it as a hash. The best way I've done so far is to Data::Dumper the hash into a string and send the string, then p the string back into a hash on the other end. So client side is something like this: # where hashtostring() puts Data::Dumper's output into a string my $hash_in_string = hashtostring(\%hash); print $sock $hash_in_string; ... and the server has something like this: # where stringtohash() take the string ...

  1. #1

    Default Best way to Send Hash to a Socket

    Hello list,

    To day I am trying to figure out the best way to send a hash to a socket
    and have the receiver be able to use it as a hash.

    The best way I've done so far is to Data::Dumper the hash into a string
    and send the string, then p the string back into a hash on the other
    end.

    So client side is something like this:
    # where hashtostring() puts Data::Dumper's output into a string
    my $hash_in_string = hashtostring(\%hash);
    print $sock $hash_in_string;
    ...

    and the server has something like this:
    # where stringtohash() take the string
    # (from hashtostring() above) and ps into a hash
    while ($new_sock = $sock->accept()) {
    while (defined($buf = <$new_sock>)) {
    foreach ($buf) {
    my %hash_from_client_string = stringtohash($_);
    ...

    That works but I am wondering if there is a better way or an already
    existent way to do that. -IE hashtostring() and stringtohash()-

    I can't use refs of course because the server will receive the text
    HASH.... from the client and that wouldn't be too useful in the server.

    TIA

    Lee.M - JupiterHost.Net
    Jupiterhost.Net Guest

  2. #2

    Default Re: Best way to Send Hash to a Socket

    On Wed, 05 May 2004 11:38:33 -0500, Jupiterhost.Net wrote: 

    use Storable qw(nfreeze);
    use MIME::Base64;

    my $hash_in_string = encode_base64(nfreeze(\%hash));

    print $sock nfreeze(\%hash), "\n";
     

    use Storable qw(thaw);
    use MIME::Base64;

    while (defined($buf = <$new_sock>)) {
    my $hash = thaw(decode_base64($buf));
    ...
    }

    Storable creates binary data, possibly with embedded newlines. I'm
    using MIME::Base64 to make it compatible with print() and readline()
    [otherwise known as <>].

    YAML is another option. It serializes data into multi-line text. I
    chose Storable for the example because I could illustrate its use with a
    minimum of code.

    Caveat: The code's not tested.

    -- Rocco Caputo - http://poe.perl.org/
    Rocco Guest

  3. #3

    Default Re: Best way to Send Hash to a Socket

    On Wed, 05 May 2004 11:38:33 -0500, net
    (Jupiterhost.Net) wrote:
     

    If you are willing to use the Net::EasyTCP module, it uses hashes to
    communicate. I find it one of it's best features.



    --
    I'm not really a human, but I play one on earth.
    http://zentara.net/japh.html
    Zentara Guest

  4. #4

    Default Re: Best way to Send Hash to a Socket

    > On Wed, 05 May 2004 11:38:33 -0500, net 
    >
    > If you are willing to use the Net::EasyTCP module, it uses hashes to
    > communicate. I find it one of it's best features.
    >[/ref]

    Storable is another commonly used module for this purpose.

    And naturally I couldn't pass up this opportunity to mention POE,
    specifically POE::Filter::Reference, which is one of the few parts of
    POE that is standalone, aka you don't need a POE kernel running to use
    it. The interface is incredibly simple, and I have used it extensively.
    Of course since you are working with sockets, then POE would be great
    for that too ;-)....

    http://danconia.org

    Wiggins Guest

  5. #5

    Default Re: Best way to Send Hash to a Socket



    zentara wrote: 
    >
    >
    > If you are willing to use the Net::EasyTCP module, it uses hashes to
    > communicate. I find it one of it's best features.
    >[/ref]

    I'll take a look thanks for the info! :)

    Lee.M - JupiterHost.Net
    Jupiterhost.Net Guest

  6. #6

    Default Re: Best way to Send Hash to a Socket



    Wiggins d Anconia wrote:
     
    >>
    >>If you are willing to use the Net::EasyTCP module, it uses hashes to
    >>communicate. I find it one of it's best features.
    >>[/ref]
    >
    >
    > Storable is another commonly used module for this purpose.
    >
    > And naturally I couldn't pass up this opportunity to mention POE,
    > specifically POE::Filter::Reference, which is one of the few parts of
    > POE that is standalone, aka you don't need a POE kernel running to use
    > it. The interface is incredibly simple, and I have used it extensively.
    > Of course since you are working with sockets, then POE would be great[/ref]

    Thanks! looks like I'm going to take a trip to cpan today :)
     
    Jupiterhost.Net Guest

Similar Threads

  1. Does Socket.send send all bytes?
    By Chris Reay in forum Ruby
    Replies: 2
    Last Post: November 4th, 08:22 PM
  2. Send Message over UNIX Socket
    By Didatus in forum PERL Miscellaneous
    Replies: 2
    Last Post: August 20th, 11:00 AM
  3. Socket State Before Call To Send()?
    By Shane McDaniel in forum UNIX Programming
    Replies: 1
    Last Post: July 2nd, 05:56 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139