Multithreaded server GUI program crashing as soon as client connects
-
@fari35 said in Multithreaded server GUI program crashing as soon as client connects:
QTcpSocket *clientSocket = tcpServer->nextPendingConnection();
Because your member variable is uninitialized. Apart from this it's completely wrong. The threaded fortune example shows you how to create a QTcpSocket out of a socketDescriptor.
-
@Christian-Ehrlicher I've removed these lines completely but it is still crashing. I'm new to Qt and I'm not getting it what is wrong here.
QTcpSocket tcpSocket; if (!tcpSocket.setSocketDescriptor(socketDescriptor)) { emit error(tcpSocket.error()); return;
and after initializing the tcpserver like this :
mythread::mythread(int socketDescriptor, const QString &fortune, QObject *parent) : QThread(parent), socketDescriptor(socketDescriptor), text(fortune) { tcpServer = new QTcpServer(this); }
I'm getting this error:
qt.core.qobject.connect: QObject::connect(QAbstractSocket, Unknown): invalid nullptr parameter qt.core.qobject.connect: QObject::connect(QIODevice, mythread): invalid nullptr parameter qt.core.qobject.connect: QObject::connect(QAbstractSocket, mythread): invalid nullptr parameter
-
Please follow and use the multi threaded QTcpServer example as I already told you.
-
@fari35 said in Multithreaded server GUI program crashing as soon as client connects:
@Christian-Ehrlicher I've removed these lines completely but it is still crashing. I'm new to Qt and I'm not getting it what is wrong here.
If you're not well versed in Qt, then you really shouldn't jump into the deep dark ocean that is threading.
Take it slow instead; make the server work without threads first, Qt's sockets are already asynchronous so you often don't need threads to begin with. -
@fari35 most Qt classes are async, so no threads are needed.
so yes, you can create a server without threads and it can handle multiple clients parallel.
threads only help if you have many clients as you can distribute the work on sevral cores.
Regards
-
@fari35
@Christian-Ehrlicher referred you earlier to the multi-threaded server example https://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.html . You should look at the single-threaded one https://doc.qt.io/qt-5/qtnetwork-fortuneserver-example.html . The latter is simpler, and should suffice unless you have further needs. At least try out it out, without multiple threads, to understand and see how you go. -
@fari35 said in Multithreaded server GUI program crashing as soon as client connects:
If I assign threads to each client when they are connected so is it the same if I use tcp sockets without threads? all the clients can connect at the same time? I mean it will behave in the same way?
That's what I wrote, yes. Unless you decide to do something very long in that main thread (which then you'd decide to offload to a worker thread instead), then I see no reason for you to thread the TCP needlessly. As a matter of fact last year I deployed a decently sized project that had TCP communication (server + clients) without making it threaded, sometimes there's just no reason to complicate it.