Since no one mentioned sprintf
you can just convert any variable to char*
using it and send
if(strcmp(buf,"movUP") == 0)
{
char* msg = calloc(1, 20);
pos.y += 0.0001f;
sprintf(msg,"NEW::POS::Y=%.4f", pos.y);
sendto(master, msg, 20, 0, (struct sockaddr*)&client, addrlen);
}
Test
movUP
NEW::POS::Y=0.0001
movUP
NEW::POS::Y=0.0002
movUP
NEW::POS::Y=0.0003
movUP
NEW::POS::Y=0.0004
Use %d
for integers, %f
for floats
to convert back to an integer, use atoi(char*)
to convert back to an float, use atof(char*)
before converting, be sure to use strstr()
to get the float value only, starting from "0"
float myPos; // floating variable that stores Object's Position in the World
...
....
memset(buf, 0, MAXBUFFER); // clears the previous buffer
recvfrom(master, buf, MAXBUFFER, 0, (struct sockaddr*)&server, &addrlen);
char* newY = strstr(buf, "0");// NEW::POS::Y=0.0001 --->> 0.000100
myPos = atof(newY); // new object's position by the server
printf("My New Position is %.4f\n", myPos); // Out: My New Position is 0.0011 -> 0.0012 -> 0.0013 -> 0.0014.
For integers (not positions), you can use the same technique and just multiply it like
float f = 0.000002f; // that is supposed to be 2 integer value
int i = (int)(f*1000000); // now, i = 2
the above methods are totally secure
If you want a more solid converting, you can use strncpy
or memcpy
and cut the string starting from a given index with some length assuming that you already know the incoming buffer, but in my personal view, I don't really recommend it, specifically in connectionless sockets like this one, lots of calculations and calls for buffer length, Not easy to debug sometimes if you're not totally aware of what you're doing.
Note 1: Be careful to not include zeros in your buffer when you're waiting for a server move/position command or any
quantity variable if you're planning to use the 1st method.
Note 2: You can just send your integer or float, convert it and vice versa without needing to cut or multiply it.
May new game networking developers find this answer useful too since we can't send or receive except char*
with UDP sendto(), recvfrom().