首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Qt QUdpSocket readyRead()信号未触发

Qt QUdpSocket readyRead()信号未触发
EN

Stack Overflow用户
提问于 2011-07-04 19:49:08
回答 1查看 4.6K关注 0票数 3

我正在尝试编写一个小型的UDP服务器应用程序。

我有一个客户端正在向这个应用程序套接字发送数据,我已经使用一个小的UDP回显程序(它将端口上接收到的数据回显到屏幕上)验证了发送正常,而且,我还可以在wireshark中看到接收到的数据包。

我使用的是QUdpSocket,似乎在设置时绑定正常-但是readyRead()信号似乎从来没有被触发过。

我已经在下面包含了我的一些代码--现在我只是想简单地模拟一下这个小的echo程序。

仅为下面的代码提供一些上下文-按下UI上的按钮将调用在UI上键入的端口上的'setupNewSocket‘。

代码语言:javascript
运行
复制
#include "sockethandler.h"

SocketHandler::SocketHandler(QObject *parent) :
QObject(parent)
{
    udpSocket = new QUdpSocket(this);

    connect( &w, SIGNAL(openNewUDPSocket(quint16)), this, SLOT(setupNewSocket(quint16)) );
    connect( this, SIGNAL(printOnUI(QString,QString,QString)), &w, SLOT(updateUI(QString,QString,QString)) );

    w.show();
}

void SocketHandler::readPendingDatagrams()
{
     while (udpSocket->hasPendingDatagrams())
     {
         QByteArray datagram;
         datagram.resize(udpSocket->pendingDatagramSize());
         QHostAddress sender;
         quint16 senderPort;

         udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);

         QString data = QString( datagram.data() );
         QString sender_address = sender.toString();
         QString sender_port = QString("%1").arg(senderPort);

         emit printOnUI(data, sender_address, sender_port);

    }

}
void SocketHandler::setupNewSocket(quint16 port)
{
    if( udpSocket->bind(QHostAddress::LocalHost, port) )
    {
    connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
    }
    else
    {
    // bind has failed
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-04 20:42:23

QHostAddress::LocalHost绑定到127.0.0.1。

您可能需要使用绑定到0.0.0.0的QHostAddress::Any

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6571057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档