QTcpSocket always connectingState
-
And where do you actually connect to an endpoint?
Ok, now there is a connect. But do you listen and accept on the server side?
-
Ok, now there is a connect. But do you listen and accept on the server side?
@Christian-Ehrlicher Sure, the server has been finished and used with other programs.
-
@Christian-Ehrlicher Sure, the server has been finished and used with other programs.
I doubt the server accepts the connection or even get a connection. Make sure there is no firewall and the server really gets a connection request.
-
I doubt the server accepts the connection or even get a connection. Make sure there is no firewall and the server really gets a connection request.
@Christian-Ehrlicher I turn off all the firewalls and try again, but it'is still in the connecting state.
-
@Christian-Ehrlicher I turn off all the firewalls and try again, but it'is still in the connecting state.
@Shen-sida
Test your server outside of a Qt program, e.g.telnet 192.168.5.181 24680
. Does that actually connect? -
@Shen-sida
Test your server outside of a Qt program, e.g.telnet 192.168.5.181 24680
. Does that actually connect?Hi,
To add to my fellow, you did not connect the errorOccurred signal.
-
@Christian-Ehrlicher I turn off all the firewalls and try again, but it'is still in the connecting state.
@Shen-sida You can also ping your server to see if it is visible.
-
I did more tests and there are some questions about that:
- when I want to reconnect the socket, there is an error id 19, what is it.
// In my tick function if(QAbstractSocket::ConnectedState != socket->state() && socketTimer.elapsed() >= 5000) { qDebug()<<"Try to reconnect socket"; socket->disconnectFromHost(); socket->connectToHost("192.168.5.233",24680); socketTimer.restart(); } // output as follow Try to reconnect socket QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "192.168.5.233" QAbstractSocket::SocketError(19)
-
I try to setup the socket in main theard and it work well! But it doesn't work if the socket setup in other thread.
-
What's the different between normal object pointer and
QPointer<>
? I'm still usingQPointer<>
to declarate this task with tcp socket
class CenterManageSystem : public QObject { ... private: QTcpSocket *socket; } class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; QPointer<CenterManageSystem> pCMS = nullptr; }; // move the object to thread pCMS = new CenterManageSystem(); CenterManageSystemThread = new QThread(); QThread::connect(CenterManageSystemThread, &QThread::started, pCMS , &CenterManageSystem::main, Qt::DirectConnection); pCMS ->moveToThread(CenterManageSystemThread); CenterManageSystemThread->start();
-
I did more tests and there are some questions about that:
- when I want to reconnect the socket, there is an error id 19, what is it.
// In my tick function if(QAbstractSocket::ConnectedState != socket->state() && socketTimer.elapsed() >= 5000) { qDebug()<<"Try to reconnect socket"; socket->disconnectFromHost(); socket->connectToHost("192.168.5.233",24680); socketTimer.restart(); } // output as follow Try to reconnect socket QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "192.168.5.233" QAbstractSocket::SocketError(19)
-
I try to setup the socket in main theard and it work well! But it doesn't work if the socket setup in other thread.
-
What's the different between normal object pointer and
QPointer<>
? I'm still usingQPointer<>
to declarate this task with tcp socket
class CenterManageSystem : public QObject { ... private: QTcpSocket *socket; } class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; QPointer<CenterManageSystem> pCMS = nullptr; }; // move the object to thread pCMS = new CenterManageSystem(); CenterManageSystemThread = new QThread(); QThread::connect(CenterManageSystemThread, &QThread::started, pCMS , &CenterManageSystem::main, Qt::DirectConnection); pCMS ->moveToThread(CenterManageSystemThread); CenterManageSystemThread->start();
@Shen-sida said in QTcpSocket always connectingState:
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "192.168.5.233"
This seems to be your problem.
"But it doesn't work if the socket setup in other thread" - why do you want to have the socket in another thread? Qt is assynchronous. If you really want to do that then you have to make sure the QTcpSocket instance is created in another thread (when CenterManageSystem::main is called).
-
@Shen-sida said in QTcpSocket always connectingState:
QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "192.168.5.233"
This seems to be your problem.
"But it doesn't work if the socket setup in other thread" - why do you want to have the socket in another thread? Qt is assynchronous. If you really want to do that then you have to make sure the QTcpSocket instance is created in another thread (when CenterManageSystem::main is called).
@jsulm said in QTcpSocket always connectingState:
If you really want to do that then you have to make sure the QTcpSocket instance is created in another thread (when CenterManageSystem::main is called).
Sure, the instance will be created in
&CenterManageSystem::main
.
Because this socket will process much data so I move it to another thread then the data can be sent by signal to update UI. -