QUdpSocket requires delays between writes
-
Hi,
Using Qt 5.14.1.
When I try to do immediate back to back writes on a QUdpSocket, only the first write makes it out (according to Wireshark). Both writes in the following code indicate success by returning 6 and 8, respectively.
#include <QCoreApplication> #include <QUdpSocket> #include <QTextStream> #include <QThread> int main(int argc, char *argv[]) { QTextStream cout(stdout); QUdpSocket myUdpSocket; 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; }
My Wireshark capture:
"1","0.000000","127.0.0.1","127.0.0.1","UDP","58","65110 → 50000 Len=6","msg 1\n"
But, if I put a delay in between the writes, both make it out (according to Wireshark).
cout << myUdpSocket.write("msg 1\n") << Qt::endl; QThread::currentThread()->sleep(1); cout << myUdpSocket.write("msg two\n") << Qt::endl;
My Wireshark capture:
"1","0.000000","127.0.0.1","127.0.0.1","UDP","58","65110 → 50000 Len=6","msg 1\n" "2","1.013918","127.0.0.1","127.0.0.1","UDP","68","50504 → 50000 Len=8","msg two\n"
Note: I also tried using "waitForBytesWritten(1000)" instead of sleep but that call returned false and only the first write made it out.
cout << myUdpSocket.write("msg 1\n") << Qt::endl; cout << myUdpSocket.waitForBytesWritten(1000) << Qt::endl; cout << myUdpSocket.write("msg two\n") << Qt::endl;
I am relatively inexperienced with Qt so my apologies if I am missing something obvious. Any help understanding why QUdpSocket and/or write() are behaving this way is greatly appreciated. Thank you in advance.
-
@Mwvse said in QUdpSocket requires delays between writes:
I am missing something obvious
Yes, since QUdpSocket is async it has no time to write all the data before it gets destroyed. Use a proper eventloop + signals and slots or (but bad style) use the waitForFoo functions.
-
Just for grins: what happens when OP takes initial post code and treats QUdpSocket as a heap object?
myUdpSocket=new QUdpSocket()
// do all writes quickly and sequentialls
// sleep()
delete myUudpSocketI'm curious when the QudpSocket object goes out of scope; after last reference or truly at end of main().
-
@Kent-Dorfman said in QUdpSocket requires delays between writes:
after last reference
What reference do you mean? Since it's a raw pointer there are no references.
Do you mean you putmyUdpSocket=new QUdpSocket()
inside main()? If so, the pointer goes out of scope when main finishes. The object itself is deleted when "delete" is called.
-
Thank you for the reply.
I am sleeping for 5 seconds before main (and thus myUdpSocket) goes out of scope.
Both '.write()' calls return the expected number of bytes written but only the first write actually gets sent.
The only difference between the example that works and the one that doesn't is a 1 second delay between calls. It's as if the Qt implementation has no internal buffering but yet the write() call indicates success.
-
Hi,
Since you are not using the asynchronous nature of Qt, you should at least use the synchronous API of QUdpSocket waitForBytesWritten.
-
@SGaist
Thank you very much for the reply.I tried that but that call merely returned false and still only the first write() made it out onto the wire.
#include <QCoreApplication> #include <QUdpSocket> #include <QTextStream> #include <QThread> #include <QHostAddress> int main(int argc, char *argv[]) { QTextStream cout(stdout); QUdpSocket myUdpSocket; myUdpSocket.connectToHost("127.0.0.1", 50000); cout << myUdpSocket.write("msg 1\n") << Qt::endl; cout << myUdpSocket.waitForBytesWritten(1000) << Qt::endl; cout << myUdpSocket.write("msg two\n") << Qt::endl; QThread::currentThread()->sleep(5); return 0; }
I did see the following note in the reference manual (I am, indeed, trying this under Windows at the moment):
bool QAbstractSocket::waitForBytesWritten(int msecs = 30000) Reimplements: QIODevice::waitForBytesWritten(int msecs). ... **Note: This function may fail randomly on Windows. Consider using the event loop and the bytesWritten() signal if your software will run on Windows.**
-
Out of curiosity, why are you not following the rule that you should first create a QCoreApplication before any QObject based classes ?
-
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). -
@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?
-
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"); }