QTcpSocket why SocketTimeoutError is raised ?
-
Hi,
I have a simple class to connect to a sever using a QTcpSocket. I test this class with QUnit.
But when I want to check that my socket is correctly open by QVERIFY(socket->open() ==true) the test fails. I just call socket->open() with no QVERIFY and the I am able to read data from my socket. But a SocketTimeoutError is raised. I d'ont know why ? And what is the cause.
The error is raised when I'am doing this :/** * @brief UniwheelConnection::isWaitingForCommand * @return true if sensor wait command */ bool UniwheelConnection::isWaitingForCommand() { QString fullData; int maxSize = 5000; //We wait end of data transmission but not too much if device is in start mode while(this->waitForReadyRead(1000) && fullData.size() < maxSize) { QByteArray data = readAll(); skipHeader(&data); qDebug()<< "data added " << data.toHex(); QString dataAsString(data); qDebug()<< "data added in string " << dataAsString; fullData.append(dataAsString); } qDebug()<< "isWaitingForCommand " << fullData; qDebug()<< "end ok " << fullData.endsWith(PROMPT_END); return fullData.endsWith(PROMPT_END) && this->atEnd(); }
Any idea why ?
-
Hi,
Why are you using open ?
The connectToHost method is usually used to setup a connection or maybe bind depending on your use case. -
sorry it lacks some informations.
My Tcpsocket is connectTohost by doing this in the constructor :m_socket->connectToHost(ipAddress,port);
And the function open call the open one of QIOdevice and return a boolean value that is set to true when signal connected is emitted.
bool WifiUniwheel::open() { qDebug() << m_socket->state(); UniwheelConnection::open(); qDebug() << m_socket->state(); return this->m_isConnected; }
May be I must not call open of super class. My aim is to have a parent class which could be instantiate as a TCPSocket or a RS232 connection. Like an interface in Java.
But by inspecting the result of my log I saw that the boolean value to know if this is open is set to true. The mystery is why this Socket error is raised ? Because I do a connectToHost and then an open ? I try this but the error raised again.
-
@CHPOG If you want to check whether open or not call https://doc.qt.io/qt-5/qiodevice.html#isOpen not open()
-
@JonB said in QTcpSocket why SocketTimeoutError is raised ?:
but he didn't respond....
This is often the case...
-
Thanks for your answer and sorry if I didn't directly try what you said @JonB .
So I try to remove all the call to open(). I call open() in my constructor of my Rs232 object and connectToHost in the object of my socket connection.
I had the same error raised.
But right to check that connection is open isOpen works fine.
But as I have this socketTimeoutError my tests fails. -
@CHPOG said in QTcpSocket why SocketTimeoutError is raised ?:
I call open() in my constructor of my Rs232 object and connectToHost in the object of my socket connection.
Why do you call open()? connectToHost() is enough.
-
@jsulm
Sorry if I am not really clear.
But I have two classes one for a RS232 which call open() in the constructor.
An other one for Socket which call connectToHost() in the constructor.
So for now I am not calling open() for Socket connection. I only use connectToHost().
And the SocketTimeouError is still raised. -
@CHPOG You wrote: "But when I want to check that my socket is correctly open by QVERIFY(socket->open() ==true) the test fails". So, you do call open() to check whether the socket is open? If so, then please change it to isOpen().
-
To be easier for everyone to understand I test with a main and get the same error.
#include <QCoreApplication> #include <QTcpSocket> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTcpSocket *socket = new QTcpSocket(); socket->connectToHost("10.1.0.1",1470); socket->write("list\r"); QString fullData; int maxSize = 5000; //We wait end of data transmission but not too much if device is in start mode while(socket->waitForReadyRead(1000) && fullData.size() < maxSize) { QByteArray data = socket->readAll(); //Removing TCP header data.remove(0,32); //qDebug()<< "data added " << data.toHex(); QString dataAsString(data); //qDebug()<< "data added in string " << dataAsString; fullData.append(dataAsString); } qDebug()<< "isWaitingForCommand " << fullData; qDebug()<< "ERROR = " << socket->errorString(); socket->close(); return a.exec(); }
I can do more simple. And I always do not understood why this error is raised.
Below this is the ouput :isWaitingForCommand "; value ; unity ; minimum value ; maximum value ; description\r\nsample_rate ; 800 ; Hz ; 200 ; 1100 ; FrÚquence d'Úchantillonnage de la centrale ADIS ; \r\nsensors ; 7 ; ; 1 ; 7 ; Capteurs actifs (BIT0=gyros BIT1=accÚlÚros BIT2=magnÚtos) ; \r\nsynchro ; 0 ; ; 0 ; 1 ; Active l'emission d'un 0x02 en dÚbut de trame ; \r\ncounter ; 0 ; ; 0 ; compteur de trame avant les donnÚes capteurs ; \r\nbt_rate ; 230400 ; ; 115200 ; 230400 ; Baudrate pour la communication bluetooth ; \r\npassword ; 0 ; ; 0 ; 999999 ; Mot de passe pour accÞs aux paramÞtres systÞmes ; \r\nsensor$ " ERROR = "Network operation timed out"
-
@CHPOG since you insist on using the synchronous api, you should at least use https://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected
for the connection to the host to be established...