简体   繁体   中英

QUdpSocket doesn't emit readyRead() signal

I faced a problem with QUdpSocket . Signal readyRead() seems to be never emitted. So, I decided to create QTimer and check state of socket reading queue. That way I ensured that socket working properly ( bytesAvailable() shows number of bytes) and signal/slot mechanism is working too (timeout() signal occurred). But why readyRead() doesn't emit? Thanks.

Qt 5.1

QString EthernetListener::listen()
{
     udp_socket = new QUdpSocket(this);
     connect(udp_socket, SIGNAL(readyRead()), this, SLOT(process_messages()));
     QTimer *timer = new QTimer(this);
     connect(timer, SIGNAL(timeout()), this, SLOT(dummy_slot()));
     timer->start(1000);
     bool res = udp_socket->bind(QHostAddress::Any, 1947, QUdpSocket::ShareAddress);
     if (!res)
         return QString("Не удалось подключиться к хосту").toUtf8();
     return QString("Идет прослушка сети. Хост: ");
}

void EthernetListener::dummy_slot()
{
    int test = udp_socket->bytesAvailable();
}

void EthernetListener::process_messages()
{
     bool bp = true;
}

This problem can occur if more data becomes available while still processing the first datagram. Add this to the slot and it should work fine:

int readCount;
while (udpSock->hasPendingDatagrams())
{
    readCount = udpSock->readDatagram(buffer, 4096);
    cout << "readCount = " << readCount << endl;
}

You can try implementing basic receiver using this one: Udp Receiver
Probably you are not reading correctly from the slot so it looks like the signal is not emitted. Or you are calling bind after you connected the slot: probably you should call bind before connecting the slot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM