Click to See Complete Forum and Search --> : Another newbie... How do I send anything else but chars ?


BlackSun
January 3rd, 2005, 12:14 PM
Ok, so I read the FAQ on how to transfer structs/creating packet schemes, but as I understand it, it still sends a string of characters, and now I'm fed up with formating everything I wanna send. So how can I send for example an int, or a made class ?
I'm using the winsock send(SOCKET s, ...... function, which is the only one I manage to find and use. but since this must be something that a billion people wants to do there must be something else, or isn't there ?

TIA

NoHero
January 3rd, 2005, 12:33 PM
There is no other funtion than 'send'. And a structure is not a string of characters: It is a string of bytes. For example: An long consists of 4 bytes. So 4 bytes will be sent. Do you understand what I mean? Everything is a string of bytes, it's just the way we interpret them to differ between a long, short, int, double etc.

BlackSun
January 3rd, 2005, 01:58 PM
Yes, I think understand what you're saying, but it doesn't help me. I still can't send an int or anything else than variables declared as an char.

What I meant about the chars being sent is that they in the example were declared as char xxxx[1024] in the struct ...

NoHero
January 3rd, 2005, 02:16 PM
Yes, I think understand what you're saying, but it doesn't help me. I still can't send an int or anything else than variables declared as an char.

What I meant about the chars being sent is that they in the example were declared as char xxxx[1024] in the struct ...

This is the additional data that will be sent within each packet.

BlackSun
January 3rd, 2005, 03:38 PM
Well of course it is, but still declared as char, so of course it can be sent. But still doesn't help me on how to send something declared as anything else ...

MikeAThon
January 3rd, 2005, 08:29 PM
Well of course it is, but still declared as char, so of course it can be sent. But still doesn't help me on how to send something declared as anything else ...

The only function available to you is send(), which requires a char pointer, so you must cast the address of "something else" to a char pointer:

CMyBigStruct bs;
// .. initialize members of bs here ..
char* cp=(char*)(&bs);
send(socket, cp, sizeof(bs), 0);

Note that sending simple integers etc might garble the value across different hardware platforms, because of big-endian vs. little-endian issues. Look into the htonl() and ntohl() functions to resolve these issues.

Mike

miteshpandey
January 4th, 2005, 10:58 AM
Originally posted by BlackSun Ok, so I read the FAQ on how to transfer structs/creating packet schemes, but as I understand it, it still sends a string of characters, and now I'm fed up with formating everything I wanna send. So how can I send for example an int, or a made class ?


Read the article carefully?

What u really need is a union.

The send(...) function will only accept string of characters but using union you can almost send any type you want. We don't have to do casting or formatting.

Hope this helps.

BlackSun
January 5th, 2005, 05:38 PM
Thanks MikeAThon, that was what I was after I think, can't believe I couldn't get that cast done myself... I know that there will be some risks with this, but I'm having a deadline and I just wanna get this assignment done, and when it's in I'm going to make the whole app. better..

miteshpandey, I saw that too, but I don't have time to read up on unions now. It's incredible how much I've forgotten about programming. But when I'm fixing my app for real I think union is the way I'm gonna go.

Thanks both of you for the answers, stay tuned for more newbie questions in a nearby future :rolleyes: