[ENet-discuss] sending and receiving structs from eNet
Ruud van Gaal
ruud at racer.nl
Mon May 2 05:00:48 PDT 2016
I haven't used Boost::serialization myself. What I made was a class around
a buffer (char []) where I have functions like AddInt(n), AddString(s) and
such, which store the binary data in the char[] buffer. For a string you
need to first store the length and then the characters myself (alternatives
are possible but this is relatively common).
The first thing though is to understand that a pointer is always 4 bytes
(in 32 bits) pointing to an address where the actual string is stored,
which is why sending that pointer value is guaranteed to fail on the other
PC, since memory addresses on that other PC are different. More a C++ thing
than Enet...
Ruud
On Fri, Apr 29, 2016 at 8:36 PM, Amir Ramezani <amir.ramezani1370 at gmail.com>
wrote:
> can i use boost::serialization?
> or what do you recommend?
> and, how can i send strings between client and the server?
> thanks
>
> ۱۳۹۵-۰۲-۱۰ ۱۱:۲۶ −۰۷:۰۰ گرینویچ, Nathan Pritchard <npr1tchard at hotmail.com
> >:
> > Quite a few things you need to consider, I'll cover (what I think are)
> > the main two:
> >
> > 1) You're not sending the strings for 'username' or 'password', etc.
> > You're sending pointers. Think about that, you're sending a (in the case
> > of your 32-bit machine) 4-byte memory address to another client.
> >
> > 2) You need to read up Endianness and understand host and network byte
> > ordering. Bytes ordering for numbers (ints, float, etc) are interpreted
> > differently by different OSs.
> >
> > Consider writing a serialization/deserialization routine for handling
> > these structs. If you want to be sending dynamically sized structures
> > with data that needs to be interpreted, you can't just memcpy
> unfortunately.
> >
> >
> > On 29/04/2016 17:52, Amir Ramezani wrote:
> >> hello all,
> >> i want to send struct using eNet, the structure definition is like this:
> >> //the structure that represents the game data to be sent
> >> typedef struct gamedata
> >> {
> >> const char *username;
> >> const char *password;
> >> int rank;
> >> bool userpass_incorrect;
> >> bool account_not_exist;
> >> const char *mac_address;
> >> const char *hdd_serial;
> >> const char *ip_address;
> >> int x;
> >> int y;
> >> int z;
> >> int health;
> >> bool is_banned;
> >> bool notify;
> >> const char *pm_user;
> >> const char *chat_user;
> >> const char *message;
> >> const char *server_message;
> >> const char *motd;
> >> bool came_online;
> >> bool went_offline;
> >> }gamedata;
> >>
> >> now, when my server receive's this struct from client, it crashe's
> >> size is the size of packet that had been received, and data is the
> >> packet content variable
> >> and gd is the structure that i want to process
> >> i use memcpy to copy from data to struct:
> >> memcpy(gd, data+size, sizeof(*data) +size);
> >> but when i receive it, and want to process it, (dirring the
> >> debugging), i just get a memory access problem
> >> what am i doing wrong?
> >> compiler: gcc 5.3.0
> >> using gnu++14,
> >> ide:code::blocks
> >> debugger: gdb
> >> operating system: windows7 ultimate 32 bit
> >> thanks
> >> _______________________________________________
> >> ENet-discuss mailing list
> >> ENet-discuss at cubik.org
> >> http://lists.cubik.org/mailman/listinfo/enet-discuss
> >
> > _______________________________________________
> > ENet-discuss mailing list
> > ENet-discuss at cubik.org
> > http://lists.cubik.org/mailman/listinfo/enet-discuss
> >
>
> _______________________________________________
> ENet-discuss mailing list
> ENet-discuss at cubik.org
> http://lists.cubik.org/mailman/listinfo/enet-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cubik.org/pipermail/enet-discuss/attachments/20160502/674dd989/attachment.html>
More information about the ENet-discuss
mailing list