PDA

View Full Version : Can use Udp Multicast Receiver in child-window?



mp33919
3rd November 2014, 21:01
Does any one know how to get data from Udp Multicast Receiver in the Mdi child window?

I am using the Qt5.2.1 in Centos6.5 machine. Currently, if I place the QUdpSocket in the main window, the slot "processPendingDatagram" could be triggered when the readReady signal is emitted. But, if I put the QUdpSocket in the child-window and tried to program a MDI applications, the QUdpSocket can not trigger the child-window.

My child-window is a QWidget window.
The codes are in child-window's default constructor:
QUdpSocket *udpSocket = new QUdpSocket;
udpSocket->bind(...);
udpSocket-> jointMulticastGroup(...);
connect(udpSocket, SIGNAL((readyRead()), this, SLOT(processPendingDatagrams()));

When I open the child-window, I could see the UpdSocket opend correctly. I knew the data is there, but never got into "processPendingDatagram".

I would like to know how I could fix this problem?

Thanks in advanced!

wysota
4th November 2014, 08:52
The socket doesn't care where you put it, it knows nothing of any widgets. If your code doesn't work then the problem is elsewhere.

mp33919
2nd March 2015, 19:02
Can anyone tell me how to solve my new problem using QUdpSocket:
Here are the code:

QUdpSocket *udpSocket = new QUdpSocket;
QHostAddress groupAddress = QHostAddress("225.0.0.1");
udpSocket->bind(QHostAddress::AnyIPv4, 45454, QUdpHost::ShareAddress);
udpSocket-> jointMulticastGroup(groupAddress);

connect(udpSocket, SIGNAL(readReady()), this, SLOT(processPendingDatagrams());

...

in processPendingDatagram()
{
QByteArray ba;
ba.resize(udpSocket->pendDatagramSize());
QHostAddress sender;
quint16 sendPort;
udpSocket->readDatagram(ba.data, ba.size(), &sender, &senderPort);

...

I found that I am not only get data from 225.0.0.1, but also get data from 225.0.0.2. Can anyone tell me why and how to eliminate the other IPs?

Thansk

wysota
2nd March 2015, 20:44
Since UDP is connectionless, I think anyone can send you datagrams. You have to filter out the ones you don't want on your own.