c++ - Qt: Is waitForReadyRead/waitForBytesWritten necessary in a synchronous UDP socket? -


the qudpsocket can used without event loop in sync mode. , found example below:

#include <qudpsocket> #include <qtextstream>  int main() {     qtextstream qout(stdout);      qudpsocket *udpsocket = new qudpsocket(0);     udpsocket->bind(3838, qudpsocket::shareaddress);      while (udpsocket->waitforreadyread(-1)) {         while(udpsocket->haspendingdatagrams()) {             qbytearray datagram;             datagram.resize(udpsocket->pendingdatagramsize());             qhostaddress sender;             quint16 senderport;              udpsocket->readdatagram(datagram.data(), datagram.size(),                                     &sender, &senderport);             qout << "datagram received " << sender.tostring() << endl;         }     } } 

my question is: waitforreadyread necessary here? can use while(1) instead if not worrying cpu consumption? if need write, necessary add waitforbyteswritten?

i used work tcp sockets in sync mode under qt, not work @ without waitforreadyread call.

yes. sockets internally use non-interrupting platform notfications, , these fire when event loop has control of thread. choices are:

  1. write asynchronous code using c++11 lambdas/closures keep code concise without need explicit helper objects. or write qt 4-style async code explicit qobjects provide handler slots.

  2. write pseudosynchronous code using waitforxxxx methods, caveat methods of qobjects running on same thread may need reentrant.

  3. use native networking apis.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -