Raised This Month: $ Target: $400
 0% 

Having trouble sending floats with socket_send


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Mind
Junior Member
Join Date: May 2005
Location: Sweden
Old 02-27-2006 , 23:54   Having trouble sending floats with socket_send
Reply With Quote #1

I am trying to send various data and especially floats with socket_send but I am having trouble doing so. socket_send seem to prefer strings and Id rather not want to convert my floats to actual text representation of the values. Does anyone have any tips about how to properly do this or some workaround? P.s I read that socket_send2 should be able to handle 0 values at least but I cant seem to find this function either, did this get dropped in version 1.65 or did this handling get included in the socket_send?
Mind is offline
Send a message via ICQ to Mind
Kraugh
Senior Member
Join Date: Jan 2006
Location: barrington, ri
Old 02-28-2006 , 00:16  
Reply With Quote #2

i think all data has to be sent this way. try this:

Code:
public socket_send_float(socket,Float:data) {    new buffer[16];    format(buffer,15,"%f",data);    return socket_send(socket,buffer,strlen(buffer)); }
__________________
"You can not restrain a fool from speaking, but nothing obliges you to listen."
Kraugh is offline
Send a message via AIM to Kraugh
Mind
Junior Member
Join Date: May 2005
Location: Sweden
Old 02-28-2006 , 00:23  
Reply With Quote #3

Hmm... that would send the float as actual text, which is what I am trying to avoid. If I could send the actual bits of the float that would save me a lot of bandwidth.
Mind is offline
Send a message via ICQ to Mind
BAILOPAN
Join Date: Jan 2004
Old 02-28-2006 , 00:38  
Reply With Quote #4

do cell packing.

Code:
new MyPacket[PACKET_LENGTH] new Float:var = 5.0; MyPacket[X] = _:var

The _: will strip the tag off and the float will be binary data. then just stuff it into the array.

Note: socket_send() will terminate on a null byte, this is fixed in 1.65 with socket_send2().
__________________
egg
BAILOPAN is offline
Mind
Junior Member
Join Date: May 2005
Location: Sweden
Old 02-28-2006 , 00:45  
Reply With Quote #5

Thank you, this was exactly what I was looking for =)

+karma for you.
Mind is offline
Send a message via ICQ to Mind
Mind
Junior Member
Join Date: May 2005
Location: Sweden
Old 03-05-2006 , 09:53  
Reply With Quote #6

I ran into one more problem, Now that 1.70 is out and socket_send2 is here (Thanks Bailopan), I thought I would continue working on this. Problem was I could not simply stuff the integers or floats into one cell like this:

MyPacket[X] = _:var (for floats)

or

MyPacket[X] = var (for ints)

Because it still only make it one byte large and the Ints are 4 bytes large (32 bit).
I solved it like this:

Code:
new myInt[4];
myInt[0] = (var&255);
myInt[1] = ((var>>8)&255);
myInt[2] = ((var>>16)&255);
myInt[3] = ((var>>24)&255);
And then simply stuffed each value in the array into the packet.
MyPacket[X] = myInt[0];
MyPacket[X+1] = myInt[1];
MyPacket[X+2] = myInt[2];
MyPacket[X+3] = myInt[3];

(a little bit more fancy than above, but the same principle)

Maybe there is a better way to do this than with logic operators but if there isn't I hope this can help.

I thought this might be helpful for someone else trying to send binary data over sockets, for me it works like a charm. Cheers!
Mind is offline
Send a message via ICQ to Mind
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:28.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode