char packet[32];
packet[0]=PROPERACTION_HEADER;
packet[1]=positionx;
packet[2]=positiony;
send(socket,packet,3,NULL);
And that's good, untill positionx < 130 && positiony < 130 (i guess). whenever player reaches position higher than those above i get a faulty values on the other end (server). Any ideas ? Cheers, P.
SENDING INTEGER / FLOAT VALUES IN PACKET
Heloo everyone, i've stuck in a place called 'dead row' - and all that because of a one single thing. The packet structure i send looks somehow like:
If the position values do not ever become negative, the first thing to do is to use an unsigned integer.
One can use bit shifting in order to quickly encode and decode data types larger than char. Note that I use unsigned char. Casting this to char when passing to send/recv, etc will remove any warnings you may get because of the unsigned/signed type mismatch. Just remember to also cast received data back to unsigned char on the other end.
One can use bit shifting in order to quickly encode and decode data types larger than char. Note that I use unsigned char. Casting this to char when passing to send/recv, etc will remove any warnings you may get because of the unsigned/signed type mismatch. Just remember to also cast received data back to unsigned char on the other end.
#include <iostream>using std::cout;using std::endl;int main(void){ long unsigned int x = 123456789; // assuming 4 bytes large unsigned char val[4]; val[0] = static_cast<unsigned char>(x); val[1] = static_cast<unsigned char>(x >> 8); val[2] = static_cast<unsigned char>(x >> 16); val[3] = static_cast<unsigned char>(x >> 24); long unsigned int y = 0; y += static_cast<long unsigned int>(val[0]); y += static_cast<long unsigned int>(val[1]) << 8; y += static_cast<long unsigned int>(val[2]) << 16; y += static_cast<long unsigned int>(val[3]) << 24; cout << y << endl; return 0;}
Got it, the strange thing is that i do the following:
(server code)
unsigned char ALLPACKETS[64];
ALLPACKETS[0] = AUTHOK;
ALLPACKETS[2] = (_u32)client.x;
ALLPACKETS[3] = (_u32)client.y;
send(client.fd, ALLPACKETS,sstrlen(ALLPACKETS),...
and
(client code)
i do recv(sock...,buffor,...)
printf("VALUEX: %d\n",(_u32 *)(bufor_in[ 2 ]));
it works perfectly, but not in the other way as send cannot take arg2 as unsigned char and even when i cast it to:
send(sock,(char *)packet,...)
declaring packet as: unsigned char[32];
server doesn't get the right value ;/
Urgh that's shitty.
(server code)
unsigned char ALLPACKETS[64];
ALLPACKETS[0] = AUTHOK;
ALLPACKETS[2] = (_u32)client.x;
ALLPACKETS[3] = (_u32)client.y;
send(client.fd, ALLPACKETS,sstrlen(ALLPACKETS),...
and
(client code)
i do recv(sock...,buffor,...)
printf("VALUEX: %d\n",(_u32 *)(bufor_in[ 2 ]));
it works perfectly, but not in the other way as send cannot take arg2 as unsigned char and even when i cast it to:
send(sock,(char *)packet,...)
declaring packet as: unsigned char[32];
server doesn't get the right value ;/
Urgh that's shitty.
well, one thing you may want to do, just for the sake of compatibility is make sure that you send your data in network byte order and when you receive it convert it back to host byte order. Basically, when you send a long, before the call to send have a line such as:
var_to_send = htonl(var);
then on the receiving end, after the call to recv:
var_recvd = ntohl(var);
doubt this is the root of your problem, but it ensures that the hosts on both ends see the number in their native endienness
var_to_send = htonl(var);
then on the receiving end, after the call to recv:
var_recvd = ntohl(var);
doubt this is the root of your problem, but it ensures that the hosts on both ends see the number in their native endienness
Okay, let's see if that helps..
I believe it's a matter send(...) construction in Win, anyway -
will post the results in a while.
I believe it's a matter send(...) construction in Win, anyway -
will post the results in a while.
Quote:
Original post by pabloes
Heloo everyone,
i've stuck in a place called 'dead row' - and all that because of a one single thing.
The packet structure i send looks somehow like:char packet[32];packet[0]=PROPERACTION_HEADER;packet[1]=positionx;packet[2]=positiony;send(socket,packet,3,NULL);
And that's good, untill positionx < 130 && positiony < 130 (i guess).
whenever player reaches position higher than those above i get a faulty values on the other end (server).
Any ideas ?
Cheers,
P.
If I get it you are limited by the size of a char, correct? Well, why don't you just sent a POD type that contains all your data instead of taking time to encode into chars and then back? Like so:
// note: I'm using a general send funcion, but you get the ideastruct packet { int posx; int posy; int type; // etc. etc.};// later onpacket p;p.posx = playerx;p.posy = playery;p.type = PACKET_TYPE;sent_packet(/*the packet*/(char*)&p, sizeof(p) /*size in bytes*/);// and when you recieve itpacket *p = recieve_packet();if (p->type == PACKET_TYPE) { playerx = p->posx; playery = p->posy;}
I hope this gives you an idea of some kind.
Green is good for teh eyes.
Nope...
So, to summarize:
sending unsigned char via send(...) works fine from unix box, but
sending the same structure from win drops known error:
error C2664: 'send' : cannot convert parameter 2 from '_u8 [512]' to 'const char *'
_u8 = usigned char.
If i'll try to do the manner:
send(sock,(char *)pakiet,sizeof(4),NULL);
my integer doesn't flow correctly.
If any idea, please drop a line..
So, to summarize:
sending unsigned char via send(...) works fine from unix box, but
sending the same structure from win drops known error:
error C2664: 'send' : cannot convert parameter 2 from '_u8 [512]' to 'const char *'
_u8 = usigned char.
If i'll try to do the manner:
send(sock,(char *)pakiet,sizeof(4),NULL);
my integer doesn't flow correctly.
If any idea, please drop a line..
Quote:
Original post by 0x0bAdBeEf Quote:
Original post by pabloes
Heloo everyone,
i've stuck in a place called 'dead row' - and all that because of a one single thing.
The packet structure i send looks somehow like:char packet[32];packet[0]=PROPERACTION_HEADER;packet[1]=positionx;packet[2]=positiony;send(socket,packet,3,NULL);
And that's good, untill positionx < 130 && positiony < 130 (i guess).
whenever player reaches position higher than those above i get a faulty values on the other end (server).
Any ideas ?
Cheers,
P.
If I get it you are limited by the size of a char, correct? Well, why don't you just sent a POD type that contains all your data instead of taking time to encode into chars and then back? Like so:
*** Source Snippet Removed ***
I hope this gives you an idea of some kind.
Yep, that is what i will have to do now, rewrite the code...
Thanks guys!
You might want to read the Forum FAQ which mentions things useful to you, such as:
- what to do about the "char *" argument to send()/recv()
- what to do when wanting to send floating-point values with known ranges
- where to go for help with "marshalling" and "serialization"
- what to do about the "char *" argument to send()/recv()
- what to do when wanting to send floating-point values with known ranges
- where to go for help with "marshalling" and "serialization"
enum Bool { True, False, FileNotFound };
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement