views:

54

answers:

2

I'm trying to send an int array through Winsocks. I might be wrong, but I'm pretty sure only a char* is supported so I'm kind of stuck on how to do this properly. There are also problems with little/big edian, so what would be a good way to do this? I've already asked a question of converting int array to char but it was recommended to start a new thread on this in the networking section instead.

A: 

Try this and this. I've just written and tested it for Windows XP 32 bit and FreeBSD i386 7.1-RELEASE. And don't forget to serialize data properly. App on Windows sends 0xAABBCCDD and app on the other end on FreeBSD recieves the same integer. It's easy to modify these apps so that they'll work with arrays. Have fun!

Yasir Arsanukaev
Thanks. I'm assuming I just plug the array in in place of the other thing you are sending?
seed
+2  A: 

You're misunderstanding what you can send using WinSock. Yes send() is defined as taking a char* buffer, but in reality it just takes a buffer of data. If send() were defined today buf would be defined as a void*. But I believe that void* wasn't part of the C standard when sent() was defined.

The summary of that is send() doesn't case what kind of pointer you pass to it, it just takes the raw bytes the pointer points to and stuffs them into the packet. It never looks at them. So there is nothing wrong just passing the array:

int sendArray(SOCKET sock, int array[], int arrayLen) {
    // You need to send the array length so the receiver knows
    // how long the array is
    int bytesSent = send(sock, (char*)&arrayLen, sizeof(arrayLen));

    if (bytesSent != SOCKET_ERROR) {
       // If that worked, send the 
       bytesSent = send(sock, (char*)array, sizeof(array[0]) * arrayLen);
    }

    return bytesSent;
}

Then you'll have to write a loop on send that gets the length of the array, allocates an array of that size and loops on recv() while it collects the array.

As long as both your client and server both have the same endianess, you don't need to do any extra processing since both machines have the same data format. Since you question is tagged Windows I assume that is the case.

shf301
Oh okay, I did have a misunderstanding there. I'll try this!
seed