Unsolved QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
-
@Christian-Ehrlicher I have read the example like three-times, but I still don't know how to fix it. Can you help me?
-
@rktech said in QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread:
but I still don't know how to fix it.
You have to override incomingConnection() as shown there.
-
@Christian-Ehrlicher said in QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread:
@rktech said in QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread:
but I still don't know how to fix it.
You have to override incomingConnection() as shown there.
I have tried, but I don't know how to implement that into my code.
-
What exactly have you tried ?
-
@SGaist To add override to my code, but I don't know where should I place it...
-
@SGaist How can I send the connection from incomingConnection() to Pool?
-
@SGaist I have successfully overrided the incomingconnection(), but I don't know, how to pass it to the Pool.
-
You'll have to redesign your pool a bit to ensure that your sockets are created in the context of the run method of your QThread subclass.
-
@SGaist I know, but how?
-
You have to transfer the descriptor to your pool and there in your run method create new sockets when descriptors are added.
-
@SGaist Should I add this part of code to my Pool::add() method? (Of course with editing it)
QTcpSocket tcpSocket; if (!tcpSocket.setSocketDescriptor(socketDescriptor)) { emit error(tcpSocket.error()); return; }
-
@SGaist I have (probably) successfully rewritten the Pool add method, but I have another problem. When I add this:
protected: void incomingConnection(qintptr socketDescriptor) override;
to myserver.h I get this error:
...\myserver.h:35: Chyba: only virtual member functions can be marked 'override'
(Chyba is Error in my local)
What I am doing wrong? -
Hi
well it is virtual https://doc.qt.io/qt-5/qtcpserver.html#incomingConnection
so does Pool inherit QTcpServer ?
The error you get says "Hey, i dont see that funtion in base class, so you cant use override" -
@mrjj No, the server is build under the myserver class
-
This post is deleted! -
@mrjj the header of server look like:
#ifndef MYSERVER_H #define MYSERVER_H #include <QObject> #include <QDebug> #include <QTcpServer> #include <QTcpSocket> #include <QString> #include <QFile> #include <QStandardPaths> #include <QThread> #include <QElapsedTimer> #include <pool.h> #include <QDateTime> class MyServer : public QObject { Q_OBJECT public: explicit MyServer(QObject *parent = nullptr); signals: public slots: void newConnection(); void onReadyRead(); private: QTcpServer *server; QList<QTcpSocket*> _sockets; QString a; QString com; QTcpSocket *socket; QList<QTcpSocket*> sl; /*protected: void incomingConnection(qintptr socketDescriptor) override;*/ }; #endif // MYSERVER_H
-
Sorry but please learn C++ first. If you want to override a function from a class you should inherit from this class. So inherit from QTcpServer, override the function and do whatever you want to. This is basic c++ stuff...
-
@Christian-Ehrlicher That's the thing, that I didn't notice. Thanks
-
@Christian-Ehrlicher But I don't know, how to edit it.
-
I have rewritten half of my project to solve the problem:
myserver.h#ifndef MYSERVER_H #define MYSERVER_H #include <QStringList> #include <QTcpServer> #include <QDateTime> class MyServer : public QTcpServer { Q_OBJECT public: MyServer(QObject *parent = nullptr); protected: void incomingConnection(qintptr socketDescriptor) override; private: QStringList fortunes; }; #endif // MYSERVER_H
myserver.cpp
#include "myserver.h" #include "pool.h" Pool p; MyServer::MyServer(QObject *parent) : QTcpServer(parent) { p.start(); } void MyServer::incomingConnection(qintptr socketDescriptor) { QDateTime qdt; p.add(socketDescriptor,qdt.time()); }
pool.h
#ifndef POOL_H #define POOL_H #include<QObject> #include<QThread> #include<QTcpSocket> #include<QDateTime> #include<QElapsedTimer> class Pool : public QThread { Q_OBJECT protected: void run(); private: struct conn{ QTcpSocket *s; QTime qdt; }; QList<conn> x; public: void add(int socketDescriptor, QTime qdt); }; #endif // POOL_H
pool.cpp
#include "pool.h" #include <QElapsedTimer> void Pool::run() { QElapsedTimer qet; qet.start(); while (true) { if(qet.elapsed()>=5000){ foreach (conn y, x) { if(y.s==nullptr){ qDebug()<<"empty"; continue; } y.s->write("test"); y.s->flush(); } qet.restart(); } } } void Pool::add(int socketDescriptor, QTime qdt){ QTcpSocket* s = new QTcpSocket; s->setSocketDescriptor(socketDescriptor); s->write("1\n"); conn c; c.s=s; c.qdt=qdt; x.append(c); }
But I have still the same error:
QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
What's wrong?