Skip to main content
GameDev.net gamedev.net
🔒 Locked

server client object/text

Started by muster Mar 3, 2009 at 5:55 AM 7 replies 1.2k views
Original Post
muster
muster
Hey all. I want to send objects from client to server as of binary form and when the server listens to the binary data it regenerate the object from binary back to its original form and use it like sending an object of player position. But i have no idea of how to do send the object and how to get its form back from binary data to normal form. Also i want to send text messages too at the same time because some message shouldn't necessary be of binary form and just text like chat messages and so on. So how can I mix between these two in a single listener and how to differentiate between them. Please reply fast and tnx in advance...
ToohrVyk
ToohrVyk
A string is an object, so once you can send objects sending strings is equally easy.

As for sending objects, required reading : [C++] [C#] [Java].
muster
muster
tnx for the link it will be helpful. Also I'm coding in c++ but how can differentiate between a data sent as binary data and normal text?
ToohrVyk
ToohrVyk
Quote:
Original post by muster
Also I'm coding in c++ but how can differentiate between a data sent as binary data and normal text?
Don't send data as text. Send everything as binary, including text data.
muster
muster
but is it good for games since i heard that long text messages are preferred to be sent as text messages instead binary. Also I made sniffing on an mmorpg packets and noticed that they send part of the data as binary messages like game data such as player state and other part as text like long text messages such as news or players lists and so on. please advice and tnx for the reply since things are now clearer especially concerning serialization.
Evil Steve
Evil Steve
Quote:
Original post by muster
but is it good for games since i heard that long text messages are preferred to be sent as text messages instead binary. Also I made sniffing on an mmorpg packets and noticed that they send part of the data as binary messages like game data such as player state and other part as text like long text messages such as news or players lists and so on. please advice and tnx for the reply since things are now clearer especially concerning serialization.
Text is binary. Just in another form.

You could send a message packet as (Psuedocode):
struct Packet{   unsigned short nLen;   unsigned short nType;   unsigned char data[1];};void send_message(const char* szMsg){   size_t msgLen = strlen(szMsg);   unsigned char* buff = (unsigned char*)malloc(sizeof(Packet) + msgLen);   Packet* pPacket = (Packet*)buff;   pPacket->nLen = msgLen;   pPacket->nType = PACKET_MESSAGE;   memcpy(pPacket->data, szMsg, msgLen);   send(pPacket);}
muster
muster
mmm ok, then there is no problem in sending all the data as binary. also there is one question, if i want to send two different objects for example one for player status and another for player chat message how can this be handled as binary data like the packet sent is of different format, on that contains struct PlayerStat{float x,y,z; char name[18];}; and another is PlayerMessage{char name[18]; char message[256];}; should a header be sent to identify the next type of packet sent in each send message in order to analyze the listened data successfully or should the whole player data be sent in one unique object that contains every player stat including chatting text if any applied? I'm new to this field and i need you help.
Evil Steve
Evil Steve
Quote:
Original post by muster
mmm ok, then there is no problem in sending all the data as binary. also there is one question, if i want to send two different objects for example one for player status and another for player chat message how can this be handled as binary data like the packet sent is of different format, on that contains struct PlayerStat{float x,y,z; char name[18];}; and another is PlayerMessage{char name[18]; char message[256];}; should a header be sent to identify the next type of packet sent in each send message in order to analyze the listened data successfully or should the whole player data be sent in one unique object that contains every player stat including chatting text if any applied? I'm new to this field and i need you help.
If you're using TCP/IP, then you just compile two seperate messages and send them. If you're using UDP, it makes more sense to compile as much state into one packet as possible, although in this case it'd probably be fine to send two separate packets, since PlayerMessage's aren't going to be very common (I.e. not sending several per second usually).

The usual way of dealing with this is the struct I showed in my psuedocode. A small header, containing the data size (Not needed with UDP), and the message type. Then the actual data follows. In your example you'd send the length and type, then the data. On the receiving end, you'd read the length and type, then read that many bytes (Again, not needed with UDP). Then, once you have the whole packet in memory you can check the type and deal with the data appropriately.
As an aside; I'd send only as many bytes as you need; not 256 for the message and 18 for the name, rather strlen(name) bytes for the name and strlen(message) bytes for the message. You could have the length of each field in the header (Which is what I'd do), or you could null terminate them.
muster
muster
tnx alot guys, now its clear. guess its time for implementation now.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.