Advertisement

sending an array of ints

Started by February 28, 2003 01:31 PM
2 comments, last by klio 21 years, 11 months ago
Ok. I''ve got an array of short ints(unsigned short Arr[640] and a UDP socket. How do i serialize the array to a bytestream and send all the data in one packet?
If it truly is an array and they truly are shorts, you can just point the socket to the array and send.

sendto(sock, (const char*) array, sizeof(array), NULL, (sockaddr*) &dest, sizeof(dest)); 


Of course, the shorts will still be in the format of the machine you happen to be run on, you might have to call htons() on all of them first.
Advertisement
I tried that, but it didn''t work... =(
Really? What type of machine and compiler are you using? What DID you receive? Did you account for endianness?

I guess I didn't really answer your question. It is my understanding that true arrays of true primitive types in c++ incur no padding since the primitives automatically naturally align.

You could check by building the send array differently:


  char *newArray = new[numElements*sizeof(short)];char *p = newArray;for (int i = 0; i < numElements; i++){  (*(short*) p) = oldArray[i];  p += sizeof(short);}  


oldArray and newArray should be the same, but you can check...

[edited by - jermz on February 28, 2003 4:07:22 PM]

This topic is closed to new replies.

Advertisement