A network packet is a string of bytes and so to put something into a packet, you first have to turn it into a string of bytes. There are lots of ways to do that, but you want to do it in a way where the other side can unambiguously turn the string of bytes that it receives back into something resembling the original data structure.

This is called marshaling.

You second example implies that the elements of your arrays are all strings of length 8 composed of "0"s and "1"s. Is this really true?

If instead of that you had an array of numbers, then you could use: send( SOCKET, pack("C*",@array), 0, $paddr ) to send those elements over the socket and then use @array= unpack( "C*", $packet ); on the other side to pull the elements back out.

However, you say "all of this data I need to send in binary (not ascii)". But there are lots of quite different meanings to the term "binary":

  1. The (text) representation of a number using base 2
  2. A string of bytes that contains bytes that aren't "just plain text"
  3. The compact native format for data that the computer uses
  4. A type of tree where each node can have two children
  5. A method for searching a sorted list by cutting the candidates in half at each step
Since we are dealing with data over a socket and because you mention "ASCII" (which is often used to mean "just plain text"), I'd think you'd mean (2) or (3). But your second example implies that you have data that is already been converted to (1). If someone told me to use "binary and not ASCII" for sending some numbers over a socket, I'd assume they meant (3) [which also implies (2)]. But I don't understand why you'd have those number in format (1) beforehand.

But if that is really what you have, then there are lots of ways to build your packet. Here are two examples:

$packet= pack "b*", join "", @array; $packet= join "", map { pack "b8", $_ } @array;
But I'd really prefer to have more information to convince me that this is really what you should be doing. (:

        - tye (but my friends call me "Tye")

In reply to (tye)Re: Sending an array through a socket! by tye
in thread Sending an array through a socket! by flipxor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.