PDA

View Full Version : Datatype transfer over qtcpsocket



goranpavles
22nd June 2016, 10:06
Hey guys, I'm taking my first few steps in qt.
I've been trying to understand how to best approach data transfers between two machines and have found many answers about text and file transfers. But I'm still having trouble to understand the basic concept of datatypes and the translation into bytes.

I understand the concept of tcp sockets and all that. I do have a client and a server console running and they communicate successfully. But what I'd like to do is send useful information for my application on each side.
Let's say a command for the server to execute, like a function or querry, and then return a class instance, a table or anything else that the application on the other end can use and fill it's gui with. How would one approach such a translation into bytes, and then back into a useful format?

Any info or hints are much appreciated

anda_skoa
22nd June 2016, 13:38
Complex communication needs two things
- a protocol so each side knows what kind of data it should expect next, e.g. command, result, notification, etc
- a format for serializing/deserializing data into/from a byte stream

Each one can be handcrafted, e.g. using QDataStream and there are also solutions that combined them or some aspects of them, e.g. https://developers.google.com/protocol-buffers/ or https://google.github.io/flatbuffers/

Cheers,
_

goranpavles
23rd June 2016, 20:08
Awesome! Dankeschoen! :)

I'll try to learn and implement the protocol buffer compiler and API in my code.
After knowing better what to look for, I also found these two simple videos explaining some basics. Hope it helps others as it did help me.

C++ Qt 85 - Binary IO basic object serialization (https://www.youtube.com/watch?v=JpmnHTM5qX8)
C++ Qt 86 - Advanced Binary IO (https://www.youtube.com/watch?v=yBzZA10Q2xg)

Cheers

Goran