QUdpSocket writeDatagram()返回错误7(无法发送消息)

时间:2016-07-26 09:05:26

标签: qt qtnetwork

我正在使用Qt app,它基本上是QTcpServer,包含多个QTcpSocketQUdpSocket个对象。现在,一旦应用程序启动,QTcpServer即已启动,目前为简单起见,QUdpSocket已启动,我每隔<5秒<*>向LAN发送广播数据使用此QUdpSocket,工作正常:

void UeMainWindow::ueSlotNetworkInformationBroadcastTimerTimeout()
{
    QList<QVariant> parameters;
    QByteArray datagram;

    parameters.append(this->ueTcpServer()->ueServerIPv4().toString());
    parameters.append(QString::number(this->ueTcpServer()->ueServerPort()));

    datagram=UePOSCommProtocol::ueEncodeCommand(UePosCommProtocolArch::UeCommand::CMD_GET_NETWORK_SETTINGS,
                                                parameters);

    if(this->ueNetworkInformationSocket()->writeDatagram(datagram.data(),
                                                         datagram.size(),
                                                         QHostAddress::Broadcast,
                                                         20000)>0)
    {
        this->ueSlotAddEventInfoLog(UeLogEntryType::INFO,
                                    tr("Newtwork information data")
                                    .append(" ")
                                    .append(datagram.toHex())
                                    .append(" ")
                                    .append(tr("sent via broadcast")));
    }
    else
    {
        this->ueSlotAddEventInfoLog(UeLogEntryType::ERROR,
                                    tr("Error sending Newtwork information data")
                                    .append(" ")
                                    .append(datagram.toHex())
                                    .append(" ")
                                    .append(tr("via broadcast"))
                                    .append(" ")
                                    .append(tr("with error"))
                                    .append(" ")
                                    .append(QString::number(this->ueNetworkInformationSocket()->error()))
                                    .append(" ")
                                    .append("(")
                                    .append(this->ueNetworkInformationSocket()->errorString())
                                    .append(")"));
    }   // if
}   // ueSlotNetworkInformationBroadcastTimerTimeout

现在,我正在尝试通过在应用运行时关闭Wi-Fi来模拟网络断开连接。该应用程序通过QNetworkConfigurationManager::onfigurationChanged(const QNetworkConfiguration &config检测到网络更改,并重新启动服务器将其绑定到localhost

void UeMainWindow::ueSlotNetworkOnlineStateChanged(bool isOnline)
{
    this->ueSlotAddEventInfoLog(isOnline?UeLogEntryType::INFO:UeLogEntryType::ERROR,
                                tr("Network online state changed").append(" ")
                                                                  .append(isOnline?tr(" ONLINE "):tr(" OFFLINE ")));
    if(!isOnline)
    {
        this->ueTcpServer()->ueRestart(QHostAddress::LocalHost,
                                       20000);
    }   // if
}   // ueSlotNetworkOnlineStateChanged

完成此操作后,我再次尝试使用第一个代码块通过QUdpSocket发送数据,但我得到error 7QUdpSocket::writeDatagram() error 为什么?甚至可以通过Broadcast / loopback发送localhost吗?

0 个答案:

没有答案