QLocalServer and QLocalSocket for simple IPC
-
Greetings, it's been a while since I've been here. Since then I've studied QT quite a lot and I can say that I'm extremely impressed with it.
This subject/question has been asked on SO, but unfortunately that website has become quite... complicated in getting answers without someone closing your question, downvote it or more.
With that being said I came here where the "magic" happens since all the QT experts are here :)
I'm using the examples found at: https://doc.qt.io/qt-6/examples-ipc.html, with a small difference that I'm sending structures instead of a simple string.
The code is essentially the same besides the QDataStream overloads as described here. I'm sending a small structure with an int and a QString.
My current OS is Windows and it's unlikely it be executed on Unix anytime soon. Communication is working "ok" from server -> client, however I`m having a bit of setback in receiving from the client -> server.
The confusion is coming from the example posted here. I`m not sure if I should replicate the same thing in the server receive function.
At the moment on the server application on the QLocalSocket::readyRead slot I have:
QLocalSocket* clientConnection = static_cast<QLocalSocket*>(sender()); if (!clientConnection) return; QByteArray rcv_data = clientConnection->readAll();
But again I repeat myself, not sure if this "correct". My questions are:
- Why the blockSize statements in the first place?
- If blockSize checking is really necessary, how to reset it after each message?
- Should I copy the same checks with blockSize in the server application as well?
Following the example I would like to have IPC functionality that allows sending/receiving both ways (the right way), without errors or disconnections 24/7.
Any clarification/help/examples are much appreciated.
-
Hi,
You should take a look at the chat example from the wiki. It uses transaction in order to handle data transmission. Granted it used JSON but it applies the same to your structure. You just need to implement the QDataStream operators for it.
On a side note, it's qobject_cast that you shall use.
static_cast does not do any type check so unless sender is already null, your if does not prevent any issue. You likely wanted to use dynamic_cast.
-
Hi,
You should take a look at the chat example from the wiki. It uses transaction in order to handle data transmission. Granted it used JSON but it applies the same to your structure. You just need to implement the QDataStream operators for it.
On a side note, it's qobject_cast that you shall use.
static_cast does not do any type check so unless sender is already null, your if does not prevent any issue. You likely wanted to use dynamic_cast.
@SGaist Thanks, it doesn't really answer my questions but it does give me a solution. So in essence all that is different is an infinite loop inside readyRead(). A bit sceptic about it but I will do some testing. On that note, what do you mean with qobject_cast? I never heard/used it; quite new to QT.
-
You can find the details in the QObject documentation.