![](wink.gif)
sending an array of ints
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?
![](wink.gif)
If it truly is an array and they truly are shorts, you can just point the socket to the array and send.
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.
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.
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:
oldArray and newArray should be the same, but you can check...
[edited by - jermz on February 28, 2003 4:07:22 PM]
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
Popular Topics
Advertisement
Recommended Tutorials
Advertisement