Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved QUdpSocket half duplex

    General and Desktop
    2
    4
    1248
    Loading More Posts
    • 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.
    • A
      Andrea last edited by

      I'm working with Qt 5.9 and I'm using a bidirectional (send and receive) QUdpSocket. How can I avoid receiving same message has just sent on the same socket?

      Here a snippet of the code

      //  Socket init
      this->UdpSocket->bind( QHostAddress::Any, ARTNET_PROTOCOL_PORT );
      
      connect( this->UdpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()), Qt::UniqueConnection );
      
      [...]
      
      void ArtNetManager::readPendingDatagrams()
      {
          QNetworkDatagram networkDatagram;
      
          qDebug("Udp datagram received");
      
          while( this->UdpSocket->hasPendingDatagrams() )
          {
              networkDatagram = this->UdpSocket->receiveDatagram();
      
              qDebug("Received datagram from IP address: %s", networkDatagram.senderAddress().toString().toLatin1().data() );
      
              this->receiveDatagram( networkDatagram.data() );
          }
      }
      
      void ArtNetManager::sendDatagram()
      {
          QByteArray ArtNet_RawMsg;
      
          ArtNet_RawMsg.append( "Test program" );
      
          //  Writes data on the UDP socket
          qint64 sentBytes = this->UdpSocket->writeDatagram( ArtNet_RawMsg, QHostAddress::Broadcast, ARTNET_PROTOCOL_PORT );
      
          if( sentBytes == -1 )
          {
              qDebug("Cannot send data on UPD socket. Error: %d", this->UdpSocket->error() );
          }
          else if( sentBytes != ArtNet_RawMsg.size() )
          {
              qDebug("Wrong number of bytes sent. Bytes sent on socket: %d, tx buffer length: %d", sentBytes, ArtNet_RawMsg.size());
          }
      }
      

      Many thanks, Andrea

      1 Reply Last reply Reply Quote 0
      • aha_1980
        aha_1980 Lifetime Qt Champion last edited by aha_1980

        @Andrea said in QUdpSocket half duplex:

        I'm working with Qt 5.9 and I'm using a bidirectional (send and receive) QUdpSocket. How can I avoid receiving same message has just sent on the same socket?

        Sockets are always bidirectional, but you can surely use them as unidirectional communication channel: Just send on one and and only listen on the other end.

        If I understand your code correctly, both sides are connected to the same program, so it acts as server and client in one. So it's no wonder that you receive the same data that you have instantly sent before.

        PS: For debugging network communication, I can only recommend you the Wireshark network analyzer: https://www.wireshark.org/

        Qt has to stay free or it will die.

        1 Reply Last reply Reply Quote 2
        • A
          Andrea last edited by

          @aha_1980 you're right, it's possible to use 2 separated sockets, but problems begin when the 2 sockets have same port, as in my case. If I create a new QUdpSocket just for transmitting purposes, and use it in my "sendDatagram" function, message is transmitted but I cannot receive anything on the RX socket.

          My target is to use just one socket, both for transmitting and receiving, and it works well, but I would like to avoid it receive the message it's has just sent. I can setup a filter to waste received message sent by the "host", but I would like to know if there is a more "direct" way to do this.

          1 Reply Last reply Reply Quote 0
          • aha_1980
            aha_1980 Lifetime Qt Champion last edited by

            @Andrea said in QUdpSocket half duplex:

            My target is to use just one socket, both for transmitting and receiving, and it works well

            That's exactly how sockets are supposed to work.

            but I would like to avoid it receive the message it's has just sent.

            Then please check your code. The message is not sent back by any black magic; it's done on purpose at the remote station.

            I can setup a filter to waste received message sent by the "host", but I would like to know if there is a more "direct" way to do this.

            No need to filter, just don't echo the received message.

            Qt has to stay free or it will die.

            1 Reply Last reply Reply Quote 2
            • First post
              Last post