Search code examples
qtqtmqtt

QtMqtt error: TransportInvalid when connect


When I use QtMqttClient source code (download from officcal git repository) in my program On Windows PC, I got a error.

QMqttClient client;
client.setPort(1883);
client.setHostname("192.168.100.3");
client.connectToHost();

I received the following error message:

ClientState:Disconnected, ClientError:TransportInvalid

From the qt documentations, it said:

The underlying transport caused an error. For example, the connection might have been interrupted unexpectedly.

Then I copy the code to another pc with Windows, it worked fine. And worked fine on a Linux PC.

Is there somethings wrong on my computer or the Settings of Windows OS ?


Solution

  • After set breakpoint and add some debug code in my program , I found that the error was in QTcpSocket, the error was occured in the Qt network model lib.

    As we know, Mqtt was baed Tcp,and in my program, the tcp can not establish connection,and err is QAbstractSocket::UnsupportedSocketOperationError, this confused me.

    Then I search on google, I found a that someone encountered the same situation as me. And I found the solution here.

    So, just add the code to the program before connect to server, the program worked well.

    /* call before connecting to network. Easiest way is, just call somewhere at the beginning of the program */
    QNetworkProxy::setApplicationProxy(QNetworkProxy::NoProxy);