Solved QUdpSocket requires delays between writes
-
Same result.
#include <QCoreApplication> #include <QUdpSocket> #include <QTextStream> #include <QThread> #include <QHostAddress> int main(int argc, char *argv[]) { QTextStream cout(stdout); QUdpSocket* myUdpSocket = new QUdpSocket; QHostAddress addr("127.0.0.1"); myUdpSocket->connectToHost("127.0.0.1", 50000); cout << myUdpSocket->write("msg 1\n") << Qt::endl; cout << myUdpSocket->write("msg two\n") << Qt::endl; QThread::currentThread()->sleep(5); return 0; }
Console output shows number of bytes written for each is successful:
And wireshark shows only the 1st write made it out:
-
Because I am naive :-) ?? I was just trying to exercise these library classes/methods to study them a bit. I did try the following suggestion someone posted elsewhere, but still got the same problem result:
#include <QCoreApplication> #include <QUdpSocket> #include <QTimer> #include <QObject> #include <QDebug> class Sender: public QObject { Q_OBJECT public: Sender(QObject *p = nullptr): myUdpSocket(new QUdpSocket(this)) { myUdpSocket->connectToHost("127.0.0.1", 50000); connect(myUdpSocket, &QUdpSocket::bytesWritten, this, &Sender::sentStuff); QTimer::singleShot(0, this, &Sender::sendStuff); // ^^^ delays sending stuff until event loop is running // and this timer event is processed } ~Sender() { } private slots: void sendStuff() { qDebug() << myUdpSocket->write("msg 1\n"); qDebug() << myUdpSocket->write("msg two\n"); } void sentStuff(qint64 bytes) { qDebug() << "Bytes sent:" << bytes; } private: QUdpSocket *myUdpSocket; }; int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); // << application object Sender s; return app.exec(); // << event loop } #include "main.moc"
Console output:
Wireshark output:
-
As you have wireshark opened, could you check a couple of things:
- That there's a bound receiver to that port
- If there isn't, could you check if an ARP query is being done after the first datagram goes out
- If there's such a query, then that's probably the answer to your question. For a detailed explanation, look here
Also note that sending datagrams below the FastSendDatagramThreshold value in quick succession while waiting for ARP to resolve may cause datagrams to be discarded:
ARP queues only one outbound IP datagram for a specified destination address while that IP address is being resolved to a media access control address. If a User Datagram Protocol (UDP)-based application sends multiple IP datagrams to a single destination address without any pauses between them, some of the datagrams may be dropped if there is no ARP cache entry already present. An application can compensate for this by calling the iphlpapi.dll routine SendArp() to establish an ARP cache entry, before sending the stream of packets.
-
Thank you very much @kshegunov for the reply.
I have tried using netcat (under Cygwin) to listen on the port (note that only "msg 1\n" datagram arrived):
Here is unfiltered packet capture around the send of the first (and only datagram). Note the time stamps around my UDP message:
I am curious about the ARP cache as I am using the loopback device "127.0.0.1".
-
@Mwvse said in QUdpSocket requires delays between writes:
Here is unfiltered packet capture around the send of the first (and only datagram). Note the time stamps around my UDP message:
You need to expand the
Info
column so you and we can see what's sent before/after the datagram. An ARP query (2 from above post) is a TCP broadcast into the subnet (I can't tell from the screenshot if that's the case here). -
Sorry. I didn't consider it because the timestamps showed multiple seconds in between and thus weren't relevant (a potentially error-prone assumption):
-
@Mwvse said in QUdpSocket requires delays between writes:
Sorry. I didn't consider it because the timestamps showed multiple seconds in between and thus weren't relevant (a potentially error-prone assumption):
Nope, it's a red herring. I don't see anything strange. Currently I have no idea why the datagram may be discarded ... Could you try another something. Modify the slot like this:
void sendStuff() { qDebug() << myUdpSocket->write("msg 1\n"); QObject::connect(myUdpSocket, &QUdpSocket::bytesWritten, myUdpSocket, [myUdpSocket] () -> void { qDebug() << myUdpSocket->write("msg two\n"); }); }
Does that work as expected?
-
I am curious about the ARP cache problem and being on 127.0.0.1.
-
[Edit: Sorry I deleted your post by accident. I feel real dumb right now ~kshegunov]
-
Yes, my bad:
void sendStuff() { QObject::connect(myUdpSocket, &QUdpSocket::bytesWritten, this, [this] () -> void { QObject::disconnect(myUdpSocket, nullptr, this, nullptr);// To prevent looping qDebug() << myUdpSocket->write("msg two\n"); }); qDebug() << myUdpSocket->write("msg 1\n"); }
-
Packet capture looks the same. Console output only shows first write.
-
This is very strange. I'm currently out of ideas ... :|
-
Going back to your comment:
"ARP queues only one outbound IP datagram for a specified destination address while that IP address is being resolved to a media access control address. If a User Datagram Protocol (UDP)-based application sends multiple IP datagrams to a single destination address without any pauses between them, some of the datagrams may be dropped if there is no ARP cache entry already present. An application can compensate for this by calling the iphlpapi.dll routine SendArp() to establish an ARP cache entry, before sending the stream of packets."
I did the following:
#include <QCoreApplication> #include <QUdpSocket> #include <QTextStream> #include <QThread> int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QTextStream cout(stdout); QUdpSocket* myUdpSocket = new QUdpSocket; myUdpSocket->connectToHost("127.0.0.1", 50000); cout << myUdpSocket->write("msg 1\n") << Qt::endl; QThread::currentThread()->sleep(1); for(unsigned u = 0; u < 100; u++) cout << myUdpSocket->write("another\n") << Qt::endl; QThread::currentThread()->sleep(5); return 0; }
As long as I have a delay after the first write, I get all 100 of the following at full speed. So ... something in the IP stack needs to get established. That first write seems to do it although it takes some time. I am wondering if since we are on 127.0.0.1 the ARP never goes out but the net result is the ARP cache gets created and then off we go.
Anyway, thank you very much for your time and expertise. I don't have a Linux box handy at the moment but I am curious if I get similar results. If I get a chance, I will try it and post the results here just for the record.
-
@Mwvse said in QUdpSocket requires delays between writes:
As long as I have a delay after the first write, I get all 100 of the following at full speed. So ... something in the IP stack needs to get established. That first write seems to do it although it takes some time. I am wondering if since we are on 127.0.0.1 the ARP never goes out but the net result is the ARP cache gets created and then off we go.
Yes, this would seem to support that hypothesis indeed, but I didn't see a TCP broadcast in wireshark. In any case something very windows specific may be going on (which has nothing to do with Qt apparently). On the other hand if this turns out to be the case, I think Qt can do better to wait for the ARP cache to be built before sending the following datagrams.
If I get a chance, I will try it and post the results here just for the record.
This would be much appreciated.