Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Not trigger readyRead in another device (same LAN) when send multicast
Forum Updated to NodeBB v4.3 + New Features

Not trigger readyRead in another device (same LAN) when send multicast

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 413 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SilentWolf
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Here is solved thread with an issue similar as yours.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Here is solved thread with an issue similar as yours.

        S Offline
        S Offline
        SilentWolf
        wrote on last edited by
        #3

        @SGaist
        I change value of group address but same
        In another device (Jetson nano), i can join successfully but cannot receive data

        d15a39b7-6e9b-439a-8f6a-04c0ab4fcbb6-image.png ![alt text](image url)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Did you check the multicast sender example and its friend the multicast receiver example ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 2 Replies Last reply
          0
          • SGaistS SGaist

            Did you check the multicast sender example and its friend the multicast receiver example ?

            S Offline
            S Offline
            SilentWolf
            wrote on last edited by SilentWolf
            #5

            @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:8888

            I change port to 45454 but same issue

            1 Reply Last reply
            0
            • SGaistS SGaist

              Did you check the multicast sender example and its friend the multicast receiver example ?

              S Offline
              S Offline
              SilentWolf
              wrote on last edited by
              #6

              @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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved