0

I wish to send float array data from between a windows client and linux server (bi-directionally). Most queries in the forum address slightly advanced issues like c++ to java etc. My array could be as large as 1024x1024. Which is the best approach to this problem? Please show by pseudocode and real code example if possible. I prefer using the native winsock library for this. Thanks


Thanks all, I think sending the floats as char array might be the simplest of all. I am not worrying about bandwidth since I won't be sending data that often, but the other recommendations seem so demanding for a quick fix.

mskfisher
  • 3,291
  • 4
  • 35
  • 48
darel
  • 128
  • 1
  • 9

4 Answers4

2

You may use protobuf library.

lollo
  • 2,289
  • 16
  • 20
1

If you want to be really portable, then sending the floats as strings might be the best way to go.

That said, if you are okay with assuming that all computers will be using the IEEE754 floating point standard (which is a valid assumption for PC/Mac/Linux programming these days) then you can just send the raw data verbatim (i.e. send each float as 4 bytes via memcpy()).

The only wrinkle is if you want to deal with machines that use different endian-ness... e.g. sending from an Intel to a PowerPC machine. In that case, one of the machines will need to reverse the ordering of the 4 bytes before it sends them (and after it receives them) so that it will still interpret them the same as its counterpart. (There is no ntohf() or htonf() function, but you can byte-swap a float manually by memcpy()ing it to a int32_t, then byte-swapping the int32_t and sending that... on reception you would recv() the data into an int32_t, byte-swap the int32_t, then memcpy() the result into a float. Note that casting-and-assignment isn't sufficient here, you must use memcpy() to copy the data from int32_t to float and vice versa -- otherwise the lack of interoperability between the floating point and integer registers on some CPU types (e.g. Intel) will bite you and give you wrong results)

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
0

Sending floats between two different boxes is not a good idea. The internal representation of floats is not guaranteed by the standard, so a bit for bit copy of a float may result in different values on the two machines.

You could convert the floats to strings and back again, but this isn't particularly efficient (for the processor or bandwidth).

Alan
  • 1,895
  • 1
  • 10
  • 9
0

As lollo says, you can use Google Proto Buffers. But if you just want to send these floats, you don't want the WHOLE complex stuff.

You can serialize the float to bytes, send them and read them on the other end. Make sure that both compilers/OSs uses the IEEE float format (which is true for mainstream compilers/OSs).

Check also this question.

Community
  • 1
  • 1
Yousf
  • 3,957
  • 3
  • 27
  • 37