1
Network / Re: Which is faster?
« on: February 21, 2014, 10:53:28 am »
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:
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?
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;
}
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?