JayChen
JayChen

Reputation: 1

How to use QUdpSocket to receive large batches of data?

I am using QUdpSocket to receive data, the peer sent 8000 UDP datagrams in a very short time, each datagram contains 1024 bytes of data

My QT code is implemented like this

connect(udp_socket, SIGNAL(readyRead()), this, SLOT(ReceiveUdp()));

void MainWindow::ReceiveUdp()
{
    QHostAddress sender;
    uint16_t port;
    QByteArray datagram;
    int datagram_len;
    
    while (udp_socket->hasPendingDatagrams())
    {
        datagram_len = udp_socket->pendingDatagramSize();

        datagram.resize(datagram_len);
        udp_socket->readDatagram(datagram.data(), datagram.size(), &sender, &port);

        temp_udp_data.append(datagram);
    }
}

But I can't receive 8000 datagrams completely, only about 500, what should I do?

I tried to use the setReadBufferSize function, but according to QT's manual, setReadBufferSize only works for QTcpSocket, not for QUdpSocket, and I didn't get a good result

Upvotes: 0

Views: 281

Answers (1)

JayChen
JayChen

Reputation: 1

by using

udp_socket->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption, QVariant(1024 * 8000));

I can receive the full 8000 datagrams, but I receive in another thread, the udp_socket's readyRead signal is bound to the start() of the UDP receive thread, but strangely, I can't do the second receive after the first receive Second reception.

I have rewritten a dynamic library based on WinSock2 and multithreading, it can work better

Upvotes: 0

Related Questions