Okay, so I'm having trouble with trying to get networking and threading to play nice together. Without holding up the GUI thread I'm trying to launch a new thread, and on that new thread communicate with a client and do intensive operations.
Originally I had the GUI make a new thread (I made a class which sublcassed QThread) but I kept getting runtime warnings upon write() because I guess the socket is tied to the server which is tied to the original GUI thread, so I was getting an error similar (but with different classes) to this: QObject: Cannot create children for a parent that is in a different thread.
(Parent is server_thread(0x6125e60), parent's thread is QThread(0x282730), current thread is server_thread(0x6125e60)
So I reworked it using the QT fortune server example and I'm still having issues (now I get the warning and it doesn't work at all), if somebody could point me to another example I can use for reference I would appreciate it, my creation is like this:
In GUI I launch the server class with:
server = new rtserver(this);
server
->listen
(QHostAddress::AnyIPv4, quint16
(PORT_NUMBER
));
server = new rtserver(this);
server->listen(QHostAddress::AnyIPv4, quint16(PORT_NUMBER));
To copy to clipboard, switch view to plain text mode
rtserver subclasses QTcpServer, in there I overrode the incomingConnection() function, and I create the thread in there like this:
st = new server_thread(mw, socket_descriptor, this);
st->start();
st = new server_thread(mw, socket_descriptor, this);
st->start();
To copy to clipboard, switch view to plain text mode
Where MW is just a pointer to the MainWindow I need for the thread to do some calculations.
Then in the server thread which subclasses QThread I simply set the socket descriptor and MainWindow pointer in the c'tor, in run() I have:
(mClientSocket->setSocketDescriptor(mSocketDescriptor)
mClientSocket = new QTcpSocket(this);
(mClientSocket->setSocketDescriptor(mSocketDescriptor)
To copy to clipboard, switch view to plain text mode
If I run it like this, I get the error above upon socket creation and the socket never receives any data.
If I don't pass "this" as a parent to QTcpSocket then simply this always returns 0 (error is gone):
bytes_avail = mClientSocket->bytesAvailable();
bytes_avail = mClientSocket->bytesAvailable();
To copy to clipboard, switch view to plain text mode
Should I or shouldn't I be passing the server thread as a parent to QTcpSocket in my thread? I feel like I should but in the Qt Fortune example they do not.
I've tested the networking code successfully with Qt (and it works with the old method, it responds to the client), but I was getting this parent runtime warning about accessing the socket from a different thread from the Qt library upon calling write() function, so I'm trying to fix that....and I can't!!!
I feel like I'm not setting up the socket correctly anymore, the previous method relied on the GUI calling "nextPendingConnection()" but with the new method that doesn't get called anymore (incomingConnection() is overridden and it just spawns a new thread and starts it, the GUI thread returns after that). I don't think there's any additional code I need in the incomingConnection() override, the fortune example doesn't have any, just makes the thread and starts it, passing in the raw socket descriptor...but this whole 'passing the raw socket descriptor' bit doesn't seem to be working in the new method..
Any advice?
NOTE: Fortune server example I mention: http://qt-project.org/doc/qt-5/qtnet...r-example.html
Bookmarks