Sign in to follow this  
kekaka

HelpHow can I send it out by winsock?

Recommended Posts

I'm newbie,and i'm learning winsock... if I've an array int array[4][5][6]={......}; how can i send it out by send()??? and how can i receive it in array??? Can anyone give me an example??? Thanks very much

Share this post


Link to post
Share on other sites
You can only send raw data, not an array. So, you'd have to do something like this:
send(sock,array[0][0],sizeof(int)*6,0);
send(sock,array[0][1],sizeof(int)*6,0);
send(sock,array[0][2],sizeof(int)*6,0);
send(sock,array[0][3],sizeof(int)*6,0);
send(sock,array[0][4],sizeof(int)*6,0);
send(sock,array[1][0],sizeof(int)*6,0);
And so on. And the receiver will have to know what size the array is. If its variable, then you'll need to send the size of the array first, and then read that value and allocate memory.

Share this post


Link to post
Share on other sites
Quote:
Original post by uavfun
If it's not too big to send all at once you could do
send(sock, array, sizeof(array), 0);

Arrays are just stored with one element after the other so this should work just fine.

Not nessecarily for multidimensional arrays. It could be a list of pointers to arrays. It depends on what your compiler does, and whether the array is allocated like that, or with several calls to new[] / malloc()

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Queasy
Don't forget to send your ints in network byte order. I think the function in winsock is htonl.

-j


You only need to order them if you plan on having a client on a machine that as a different endian than the one you're using, no ?

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Not nessecarily for multidimensional arrays. It could be a list of pointers to arrays. It depends on what your compiler does, and whether the array is allocated like that, or with several calls to new[] / malloc()

I don't believe the standard allows that, among other things you wouldn't be able to convert a pointer to the array to an int* if it weren't consecutive, this definitely is guaranteed to work (see e.g. The C++ Programming Language section C.7).

Share this post


Link to post
Share on other sites
Multi-dimensional arrays are guaranteed contiguous, IF they are allocated as such.

int a[2][3][4]; <-- guaranteed contiguous
int **a[2]; <-- guaranteed not contiguous

If you're using UDP, sending things in different calls to send() is perilous, because you might drop some packet in the middle (each send() is a separate packet in UDP).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
thx for your all reply........
but i still don't know how to do~
can anyone give me a link for a real example code?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this