1

I have two programs that communicate with each other through TCP/IP by exchanging commands and data (connection is already successful). I'm looking for a protocol through which I can exchange commands (ASCII) and binary data (float,double,int,char,wchar_t, etc...).

Is there some standard for that, that I could use? or should I just invent my own protocol for exchanging stuff?

The program is written in C++ and Qt.

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
  • @user1929959 I'm sorry, that's not my question. I'm asking about a way to interpret the data correctly between the client and the server when exchanging bytes. – The Quantum Physicist Feb 27 '13 at 10:49

3 Answers3

1

If you are using Qt look at QDataStream http://doc.qt.io/qt-4.8/qdatastream.html

example: http://doc.qt.io/qt-4.8/qt4-network.html

jesterjunk
  • 2,342
  • 22
  • 18
kassak
  • 3,974
  • 1
  • 25
  • 36
  • Thank you for your reply. QDataStream transfers bytes blindly without fixing a type. My question is, how can the client know what data type the server has sent. Is there a known protol for that? – The Quantum Physicist Feb 27 '13 at 11:15
  • @SamerAfach Do you really need types to be sent? For example if you've sent some command, you know it's arguments and you can directly read those types. Can you calrify your needs a bit? – kassak Feb 27 '13 at 11:35
  • I may need to send a single or an array of integers, doubles, strings or anything through this connection. The other side has to receive the information and interpret it correctly and store it and use it for plotting or any other task. – The Quantum Physicist Feb 27 '13 at 11:37
  • you may try to use `QVariant` http://qt-project.org/doc/qt-4.8/qvariant.html It may be written to `QDataStream` – kassak Feb 27 '13 at 11:46
  • I'm sorry, I may not have clarified the situation correctly. Imagine that I have an array of doubles with 100 elements, and another array of floats with 200 elements that I have to send you. Now over a TCP connection you do not defined types, but you simply send "bytes", which means that if I send you any of those two arrays, the information will not be sufficient for you to know how to interpret the data (as doubles or floats, or perhaps some other type). So my question is: is there a standard for communicating data with types and lengths and all information necessary to interpret the data. – The Quantum Physicist Feb 27 '13 at 12:01
  • 1
    I've understood you. There are many protocols for such typed exchange JSON, BSON, etc. Look here http://en.wikipedia.org/wiki/Comparison_of_data_serialization_formats – kassak Feb 27 '13 at 12:11
1

I would suggest to use header+data to describe these kind of messages. In the header you could have e.g. a message type field and according to its value you can cast the content of data. I think it is not so complicated to implement. When you have this, you can exchange structures like this through whatever you want (e.g. QDataStream)

Gandras
  • 23
  • 3
1

If you're using Qt, this might be helpful.

In general, you should pick a byte-by-byte format and then serialize and deserialize into that on each side of the socket. Otherwise you eventually run into a problem with either endianness or varying sizes between OSs (usually 32 bit vs. 64 bit systems).

john.pavan
  • 910
  • 4
  • 6