And what if you don't know how many things you'll have to send?
Then you include an integer that stores the number of objects.
sf::Packet can be used in many ways, similar to binary files with std::fstream. The only thing you need is to define a format (or protocol in networking terms) that defines how the data is layed out. Concretely, this determines how the values are arranged inside a packet, and of what types they are. For example:
[MyEnum] Packet type
[Uint32] Number of objects
[float] x position of 1st object
[float] y position of 1st object
[Int32] hitpoints of 1st object
[float] x position of 2nd object
[float] y position of 2nd object
[Int32] hitpoints of 2nd object
...
You can also overload operator<< and operator>> for own types (such as an Object class in this example) to simplify the code.
Btw, great book on SFML :D
Thank you :)
By the way, if you need an example: Such a possible protocol is explained in the Network chapter of our book.
I have a similar question, I have a program that receives data, sent from a smarthphone, from a TCP/IP socket. I can not change the way the data is sent. The data is sent as a byte(char?) array and contains some floats, and some text. It looks something like
char[] = "4932942 ABC -0.123251515 0.12414 155.230420". I extract the floats from the string and process them in my program. The problem is that the size of this array can differ from sample to sample. So if I elect to use a byte array I end up with the array containing parts of more than one sample, which is unwanted because I want to receive one sample at a time, process it, and then receive the next one. The code I use to make sure I receive one message at a time is the following:
std::string sample = "";
char data = '1';
std::size_t received;
while(data != '\n') {
client.receive(&data, 1, received);
sample += data;
}
According to Nexus' previous answer in this thread this way is inefficient. As I stated before, I can not control in any way how the data is sent. It would have been nice to just receive the sample as a package but I could not make it work since it is sent from, I believe, a Java program.
Is there an easy solution to my problem? That is, to receive each sample, and nothing more, with one call to the receive function?