Search code examples
perlsocketsudpsendstorable

Send Data packages over UDP


I'm trying to send a scalar value over socket which I have got with nfreeze from storable. Step by step:

  1. I get the scalar $serializedHash = nfreeze \%hash;
  2. I want to send it over socket $sendSocket->send($serializedHash);

This works fine, as long as the scalar $serializedHash is not bigger than 1024byte. Because I have on the other side a socket which can only receive data with max. length of 1024byte. I also cannot store $serializedHash in a file and then handle it with sysread and syswrite.

What I don't want is to send every single line per socket, bacause my Hash has over 2 million entries. The hash element are seperated by a \n so I tried with the split function, but then I have a array with 2 million entries.

How can I send data in packages through a UDP socket?


Solution

  • You may possibly process your serialized hash by chunks, like that:

    while ($serializedHash =~ /(.{1,1024})/sg) {
      my $chunk = $1;
      # sending $chunk
    }