Advertisement

Send Function.

Started by June 18, 2004 12:52 PM
10 comments, last by kdogg 20 years, 7 months ago
Hi everyone! im new in winsock and i wondering if it is posible to send (int, float) values over the send()/recv() func not the orginal message variable char*. know i sounds little noobish, but I am ^^ anyway I hope anyone can help me =) thx. EDIT : my first post :S
Quote:
Original post by zyron

Hi everyone! im new in winsock and i wondering if it is posible to send (int, float) values over the send()/recv() func not the orginal message variable char*.

know i sounds little noobish, but I am ^^

anyway I hope anyone can help me =)

thx.

EDIT : my first post :S


I know unix network programming, and winsock is supposedly very similar. Is it not a (void *) you are casting to a (char *)? You'll need to change the number of bytes needed to hold your data, but you should be able to cast the data to whatever you want (at least on unix). At least if it works like unix networking, which I'd think it would.

EDIT: I checked msdn, and it is not, in fact, the same. You use a character buffer to send/recv. That shouldn't matter though, it just parses it differently (8 bits at a time). You can still actually use the data as the original data type.
Advertisement
allrigt... but i dont send the (void *) i do like this...

char *buffer;
strcpy(buffer, "Hello");
send(sock, buffer, strlen(buffer), 0);

but i whant to send int/float values not the char *buffer.. like this.


float buffer = 5.0;
send(sock, buffer, strlen(buffer), 0);

and i get this.
'send' : cannot convert parameter 2 from 'char' to 'const char *'

how can i convert the buffer so i can send it?



Yes, it is a char* in winsock, but it makes no difference. Ints and floats are (usually) 32 bits in memory, so just use a cast:

int foo = 0;float bar = 1.0f;send(socket, (char*)&foo, sizeof(int), 0);   // write an intsend(socket, (char*)&bar, sizeof(float), 0); // write a float


Or if you don't like C-style casts, use a reinterpret_cast :P.
DukeAtreides076

it was that i looking for so I understand now.

thanks.
DukeAtreides076

but if i send the int value then the server gets the char of the int like I send 62 it will be 'A' on the server.. I whant it to recv the same not the char. do I need to edit the recv to so it recv int not char?
Advertisement
Quote:
Original post by zyron

DukeAtreides076

but if i send the int value then the server gets the char of the int like I send 62 it will be 'A' on the server.. I whant it to recv the same not the char. do I need to edit the recv to so it recv int not char?


That's a problem you'll have to work around. You always send a stream of characters, it's all in how you interpret the data at the other end.

int speed = 0;
// thats the int you want to recieve and that you sent
recv(sock, (char *)&speed, sizeof(speed), 0);

That should stick the value that you sent from the other end into the integer speed.
Try this too:

 struct My_st {       int a;      char m; }; My_st buffer; send(sock,(char*)&buffer,sizeof(My_st));


This means you can use structure,classes,almost anything if you typecast it.You can recive the struct by replacing send() with recv().

Helps?
______________________________________________________________________________________________________
[AirBash.com]
Do not forget that the byte order might be different on different machines. if you don't know what I mean, just forget what I said. Hmm, that really makes my post quite stupid.

This topic is closed to new replies.

Advertisement