Jump to content
  • Advertisement
Sign in to follow this  
kekaka

HelpHow can I send it out by winsock?

This topic is 5133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
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.

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
Yeah, I was reffering to the way hplus0603 mentioned. If the array can be resized, it'll have to be allocated like that. Otherwise, theres no problem sending it all in one go.

Share this post


Link to post
Share on other sites
to the AP: yes that's right. It's always good to be as compatible as possible anyway. Why alienate pepople just because they don't use a cpu of the same endianess as you?

-j

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!