UDP Socket do not need to run in forever loop(solved)
-
Below is my udp code.
Do i need to use a thread to read processPendingDatagram() continuously or
socket constructor will always receive Signal whenever and run processPendingDatagrams()
thus forever loop not needed.@
void socketudp::processPendingDatagrams()
{while(udpSocket->hasPendingDatagrams(); { QByteArray datagram; datagram.resize(updSocket->pendingDatagramSize()); udpSocket->readDatagram(datagram.data(), datagram.size()); qDebug<<"UDP received"; }
}
socketudp::socketudp()
{
udpSocket=new QudpSocket(this);
udpSocket->bind(10010, QUdpSocket::ShareAddress);
connect(udpsocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
}
@[edit: added missing coding tags @ SGaist]
-
Hi.
First of all, why are you using the last connect? That line doesn't do anything.
Second, you can use a thread and connect a slot in the udpsocket's signal called "readyRead". Everytime udpsocket is receiving data, it calls this signal, so you can read the data incoming.
Here is a link to a solution like the one i told you about: "link":http://stackoverflow.com/questions/21224894/qt-qudpsocket-readyread-signal-and-corresponding-slot-not-working-as-supposed
I hope i have helped you.
-
I found the site below and follow what they are doing.
http://doc.qt.digia.com/4.6/network-broadcastsender.htmlI found my problem is because i created the socketUDP class object inside
Mainwindow constructor, application cannot received.
When i put the object creation outside mainwindow (in main.cpp), data is received successfully. Thanks