AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Having trouble sending floats with socket_send (https://forums.alliedmods.net/showthread.php?t=24644)

Mind 02-27-2006 23:54

Having trouble sending floats with socket_send
 
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?

Kraugh 02-28-2006 00:16

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)); }

Mind 02-28-2006 00:23

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.

BAILOPAN 02-28-2006 00:38

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().

Mind 02-28-2006 00:45

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

+karma for you.

Mind 03-05-2006 09:53

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!


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

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