Not trigger readyRead in another device (same LAN) when send multicast
-
1/ PC1:
#define MULTICAST_PORT 8888
#define MULTICAST_IP "224.255.255.1"
onJoinMulticast(MULTICAST_IP, MULTICAST_PORT)UdpMulticastAc::UdpMulticastAc() { QString hostName = QHostInfo::localHostName(); QString ip = getLocalIp(); qDebug() << "Host name: " << hostName << ", Host IP:" << ip; m_udpSocket = new QUdpSocket(this); //Socket for communication //Multicast routing level, 1 means only in the same LAN //Multicast TTL: Time to live, it will be reduced by 1 every time a route is crossed. Multicast cannot cross most routes, so it is 1 //The default value is 1, which means that the data packet can only be transmitted in the local subnet. m_udpSocket->setSocketOption(QAbstractSocket::MulticastTtlOption, 1); connect(m_udpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onSocketStateChange(QAbstractSocket::SocketState))); onSocketStateChange(m_udpSocket->state()); connect(m_udpSocket, SIGNAL(readyRead()), this, SLOT(onSocketReadyRead())); } void UdpMulticastAc::onJoinMulticast(QString ip, quint16 port) { qDebug() << "onJoinMulticast ip = " << ip << ", port = " << port; m_groupAddress = QHostAddress(ip); if (m_udpSocket->bind(QHostAddress::AnyIPv4, port, QUdpSocket::ShareAddress)) { // bind port m_udpSocket->joinMulticastGroup(m_groupAddress); // Join the multicast group qDebug() << "[user: " << getLocalIp() << "] Join multicast (multicast address: " << ip << " port: " << QString::number(port) << ")successfully!!!"; } else { qDebug() << "[user: " << getLocalIp() << "] Join multicast (multicast address: " << ip << " port: " << QString::number(port) << ")failure!!!"; } } void UdpMulticastAc::onSocketReadyRead() { qDebug("onSocketReadyRead"); while (m_udpSocket->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(m_udpSocket->pendingDatagramSize()); QHostAddress peerAddr; quint16 peerPort; m_udpSocket->readDatagram(datagram.data(), datagram.size(), &peerAddr, &peerPort); // Read data packet, message + Ip and port from QString str = datagram.data(); QString peer = QString("[From: %1 %2] %3").arg(peerAddr.toString()).arg(QString::number(peerPort)).arg(str); qDebug() << peer; }
2/ PC2: send message to group address
//Unicast message void ExTrans::on_btnUnicast_clicked() { QString targetIp = ui->comboBoxIp->currentText(); QHostAddress targetAddr(targetIp); // Target Ip quint16 targetPort = ui->spinBoxPort->value(); // Target port QString msg = ui->lineEdit->text(); // Message sent QByteArray str = msg.toUtf8(); m_udpSocket->writeDatagram(str, targetAddr, targetPort); // Send datagram ui->plainTextEdit->appendPlainText(QString("[Send: ] %1").arg(msg)); ui->lineEdit->clear(); ui->lineEdit->setFocus(); }
I tried this code in same PC, different applications: OK
But NOT OK wit different PCs -
Hi,
Here is solved thread with an issue similar as yours.
-
Hi,
Here is solved thread with an issue similar as yours.
@SGaist
I change value of group address but same
In another device (Jetson nano), i can join successfully but cannot receive data
-
Did you check the multicast sender example and its friend the multicast receiver example ?
-
Did you check the multicast sender example and its friend the multicast receiver example ?
@SGaist yes, maybe only different with group port
45454 and 8888
How to find proper group port and IP in this case? I'm using 239.255.255.255.250:8888I change port to 45454 but same issue
-
Did you check the multicast sender example and its friend the multicast receiver example ?
@SGaist I found solution for it.
I use share network from my PC to jetson nano device so it make different subset: 10.42.0.* and 192.168.1.*
After plug both PC and jetson nano with same router, it worked
Thanks for your support