QtMqtt error: TransportInvalid when connect

QtMqtt error: TransportInvalid when connect

当我在 Windows PC 上的程序中使用 QtMqttClient 源代码(从官方 git 存储库下载)时,出现错误。

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

我收到以下错误消息:

ClientState:Disconnected, ClientError:TransportInvalid

根据qt文档,它说:

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

然后我用 Windows 将代码复制到另一台电脑,它工作正常。在 Linux PC 上运行良好。

我的电脑或 Windows OS 的设置有问题吗?

在我的程序中设置断点并添加一些调试代码后,我发现错误在QTcpSocket中,错误发生在Qt网络模型库中。

众所周知,Mqtt是基于Tcp的,而在我的程序中,tcp无法建立连接,err是QAbstractSocket::UnsupportedSocketOperationError,这让我很困惑。

然后我在google上搜索,发现有人遇到和我一样的情况。我找到了解决方案 here.

因此,只需在连接服务器之前将代码添加到程序中,程序运行良好。

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