我正在尝试编写一个小型的UDP服务器应用程序。
我有一个客户端正在向这个应用程序套接字发送数据,我已经使用一个小的UDP回显程序(它将端口上接收到的数据回显到屏幕上)验证了发送正常,而且,我还可以在wireshark中看到接收到的数据包。
我使用的是QUdpSocket,似乎在设置时绑定正常-但是readyRead()信号似乎从来没有被触发过。
我已经在下面包含了我的一些代码--现在我只是想简单地模拟一下这个小的echo程序。
仅为下面的代码提供一些上下文-按下UI上的按钮将调用在UI上键入的端口上的'setupNewSocket‘。
#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
}
}发布于 2011-07-04 20:42:23
QHostAddress::LocalHost绑定到127.0.0.1。
您可能需要使用绑定到0.0.0.0的QHostAddress::Any。
https://stackoverflow.com/questions/6571057
复制相似问题