0

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

  • To add, the 8000 datagrams of 1024 bytes can be seen through Wireshark, and the sending time is only 0.0707 seconds. Should the hardware be slower? – JayChen Apr 13 '23 at 06:37
  • Does this answer your question? [How to send and receive large data using Qt UDP socket?](https://stackoverflow.com/questions/15476469/how-to-send-and-receive-large-data-using-qt-udp-socket) – Lady Be Good Apr 13 '23 at 08:51
  • Add debug prints. How many times does the slot get called? How many datagrams does it receive with each call? – hyde Apr 13 '23 at 10:43
  • Connect the other signals of the connection. Do you get any other signals except readyRead? – hyde Apr 13 '23 at 10:44
  • Since udp is stateless - are you sure all 8000 packets really arrive at the receiver side? Are all buffers on the way from the sender to the receiver big enough that no packet gets discarded? I would say no. If you need to receive all packets use a protocol which can not loos packets like tcp. – chehrlic Apr 13 '23 at 16:07
  • Thanks to @chehrlic 's comment, it is indeed a problem that the buffer is not big enough – JayChen Apr 17 '23 at 08:14

1 Answers1

0

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