[ENet-discuss] Sending numerical values in packets

Mika Halttunen lsoft at mbnet.fi
Sat Nov 8 06:54:40 PST 2003


> To answer your question, you'll want to take all our data, convert it 
> to a standardized network byte order (e.g. big-endian), serialize, 
> send, and then do the opposite on the other side.  You can use the 
> standard functions such as htons, etc. or you can roll your own.

Thanks, I got it working. The problem was in my casting, but now it 
seems to work. Is this the correct way?
----------------------------------
// Sender:
char array[10] = "";
enet_uint32 i = 23212;
enet_uint32 ni = ENET_HOST_TO_NET_32(i);
memcpy(array, (void*)&ni, 4);

ENetPacket *packet = enet_packet_create(array, 4, 0);

----------------------------------

// Receiver:
enet_uint32 ni = 0, i = 0;
memcpy((void*)&ni, (void*)&(event.packet->data[0]), 4);
i = ENET_NET_TO_HOST_32(ni);
printf("Got int: %d\n", i);

----------------------------------

At least it works on Windows with Mingw. I just hope that memcpy() won't 
slow it down :/

> Check out www.poshlib.org for a simple library (header + C file) that 
> handles endianess conversion and serialization.

Btw, the download links didn't seem to work when I tried.


-- 
Mika Halttunen
lsoft at mbnet.fi




More information about the ENet-discuss mailing list