WinSuck, can only send a char* ?

Started by
51 comments, last by Wickeeed 20 years, 7 months ago
Hi everybody, i realy NEED help. can someone say me how i can send() variables like INT and FLOAT.. ???
Advertisement
Open up your C book, read the section on pointers. Especially the bits about the & (adress-of) operator.

Then flip to the section on typecasting and read that.

Viola!
typedef struct MSG{  int nMsg;  char szMsg[MAX_TEXT_LEN]; }*PMSG;MSG hello;hello.nMsg = 1;sprintf(hello.szMsg, "Hello World!");send(hSock, (char*)hello, sizeof(MSG), 0);  


[edited by - jonstelly on September 12, 2003 2:32:24 PM]
thx guys for trying to help me but I DONT GET IT,
how i can send() variable like int i=123;
or maybe i can convert a char* in int or float and back?,

something like int yo = (char)(charvariable);
Or maybe you can convert a pointer to i into a char* like the A.P. suggested ... don''t you think ?


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Yeah, time to bust out your C/C++ book and look up type conversion.
heh heh heh
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Well, the posters above were a little mean, but when you didn''t look up what was said, when even a google on what the first AP said would have told you what you needed, you asked for it. Anyways, what you''re looking for is this:

int AnInt;
char* Pointer = (char*)&AnInt

That''ll give you a char* wherever you need one, for both sending and recieving. You can use it on anything, just replace AnInt with whatever you want.

tj963
tj963
...and before you make a new thread with: damn my send isnt working with ints floats... you will have to do something like:
int i= 3.15159;
char *c= (char *)&i
send(hSock, c, sizeof(int), 0);//line stolezd and edited

dont copy and paste this code (copyright ), but if you are working with UPD you will produce a huge ammount of small packets causing to make your transfers very very inefficient...


T2k
and now let''s throw htons() and htonl() into the mix for sending short integers and long integers across the wire.

And pointing him to a book or reference for details of type conversion isn''t rude. Giving someone a code snippet of something like pointer manipulation and conversion without them understanding it doesn''t help beyond their immediate problem if they don''t understand what they''re doing.

This topic is closed to new replies.

Advertisement