开发者

Qt QUdpSocket readyRead() signal is not triggering

I am trying to write a small UDP server application.

I have a client transmitting to this applications socket and I have verified this is sending ok using a small UDP echo program (which echoes the data received on a port to the screen) and also, I can see the packets received in wireshark.

I am using QUdpSocket and it would appear that this Binds ok on setup - but the readyRead() signal doesn't ever seem to get triggered.

I have included some of my code below - at the minute I am simply trying to emulate the little echo program.

Just to give some context to the below code - a button press on the UI calls 'setupNewSocket' on a port that is typed in on UI.

#incl开发者_开发问答ude "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
    }

}


QHostAddress::LocalHost binds to 127.0.0.1 .

Probably you need to use QHostAddress::Any which binds to 0.0.0.0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜