I’m lead to believe that write() can only send data buffers of byte (i.e. signed char), so how do I send an array of long integers using the C write() function in the sys/socket.h library?
Obviously I can’t just cast or convert long to char, as any numbers over 127 would be malformed.
I took a look at the question, how to decompose integer array to a byte array (pixel codings), but couldn’t understand it – please could someone dumb it down a little if this is what I’m looking for?
Follow up question:
Why do I get weird results when reading an array of integers from a TCP socket?
the prototype for write is:
so while it writes in units of bytes, it can take a pointer of any type. Passing an
int*will be no problem at all.EDIT:
I would however, recomend that you also send the amount of integers you plan to send first so the reciever knows how much to read. Something like this (error checking omitted for brevity):
NOTE: if the array is from dynamic memory (like you
malloced it), you cannot usesizeofon it. In this case count would be equal to:sizeof(int) * element_countEDIT:
As Brian Mitchell noted, you will likely need to be careful of endian issues as well. This is the case when sending any multibyte value (as in the count I recommended as well as each element of the array). This is done with the:
htons/htonlandntohs/ntohlfunctions.