I tried searching everywhere but no luck. Here is my problem:
I have a UDP socket, which can send data to any IP address through any port (tested and confirmed working).
I read many tutorials, but they were all manipulating char*[], they did not specify how to decrypt it.
What I would like to do (pseudo code):
Client:
Class my_class;
send (..., my_class, sizeof(my_class),...)
Server:
receive (sender, buffer, sizeof (buffer))
and do something like
Class my_class = (Class) buffer
So I my server can analyze the content of the buffer.
But I’m lost with pointers, and all I can send is char*[], so I don’t know how to convert back and forth.
You can’t just “send” your class. You have to send a representation of the data in your class. And sending a pointer to your class won’t work either. When another application on the network receives your pointer, it will be meaningless to them.
Consider this:
You can’t just “send the class” down the wire. If you tried to do something like this:
…you will end up sending nonsense to the downstream client.
value_might be received properly, butname_surely won’t. This is because astd::stringobject consists of much more than just the characters that make up the string. There are also counters, pointers to data buffers, and who knows what else. The characters themselves are probably not even going to be in the memory space indicated by(&data, &data[sizeof(data)])— those characters will be somewhere else entirely. So with thesendpsudocude above, not only do you send a bunch of stuff that the client can’t understand, very often you don’t even send what they can understand.Enter serialization. Serialization simply means creating a representation of your data object that can be stored, saved or sent someplace, and reassembled later. You decide what shape this serialization will take. In the case of
dataabove, we might decide to serialize like this:where each character is 1 byte, and:
name_in ASCII representationname_value_in ASCII representationOne way to serialize & send
dataabove might be something like this (warning: not production quality, untested):Now, instead of trying to send
data, we’re sending a character string that representsdata. Ifdatawas created like this:…then the string that gets sent down the socket will be:
This can be received by the client and reassembled in to a new
Dataobject on the client side that mimics what you have on the server side.The mapping we used above — the
NNNNCCCCCCCC...VVVV— must be understood by both sides. This is often referred to as a communications protocol.There are countless protocols and serialization methods used across all application domains. They range from the uber simplistic, like the one I outlined above, to highly compressed and complex, like FIX/FAST. Many libraries provide serialization capabilities that meet the needs of a wide range of applications. Boost.Serialization comes to mind, and I’d suggest you look in to that.
There is much more to this that I’ve glossed over here. Things like Endianness, security, session control… If you are going to be doing any significant network programming, either on the client side or the server side, you’ve got quite a bit of learning ahead of you.