1

I'm using QT. I need to broadcast data, so I try to use QUdpSocket. But data can be too big(after writeDatagram QUdpSocket::error returns DatagramTooLargeError). So I split data and call writeDatagram several times to the parts. But Received socket receive data only once, only first packet. Receive code is

connect(&m_socketReceiver, &QUdpSocket::readyRead, this, &LocalNetSharing::onDataRead);

void LocalNetSharing::onDataRead()
{
while (m_socketReceiver.hasPendingDatagrams())
{
    QByteArray datagram;
    datagram.resize(m_socketReceiver.pendingDatagramSize());

    m_socketReceiver.readDatagram(datagram.data(), datagram.size());
    //process data
}
}
Nejat
  • 31,784
  • 12
  • 106
  • 138
Hate
  • 1,185
  • 3
  • 12
  • 24
  • 1
    There is no guarantee UDP packets will be delivered. There is also latency issues you need to consider. If the datagram is not delivered immediately your `while` loop may not see it in time and bail out. – Captain Obvlious Jun 18 '14 at 21:03
  • 2
    @CaptainObvlious I understand that while loop can not see it, but in that case another readyRead signal should be emitted, I believe – Hate Jun 19 '14 at 06:20

1 Answers1

1

From the Qt documentation about QUdpSocket Class :

Note: An incoming datagram should be read when you receive the readyRead() signal, otherwise this signal will not be emitted for the next datagram.

So it seems that you are not reading the entire datagram in each call of onDataRead.

You don't specify host and port in readDatagram. I am not sure if it is the reason but the correct form is :

while (m_socketReceiver.hasPendingDatagrams())
{
     QByteArray datagram;
     datagram.resize(m_socketReceiver.pendingDatagramSize());
     m_socketReceiver.readDatagram(datagram.data(), datagram.size(), host, &port);
     //process data
}
Nejat
  • 31,784
  • 12
  • 106
  • 138