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. UDP Unicast
Forum Updated to NodeBB v4.3 + New Features

UDP Unicast

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 5.2k Views 1 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.
  • B Offline
    B Offline
    bryang
    wrote on last edited by
    #1

    Can the QUdpSocket class be set up to natively support UDP Unicast communications? I can't seem to find anything in the documentation...or maybe I just don't know what I'm looking for. An example would be great.

    Thanks!

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      Unicast is the default model. You don't need to setup anything.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bryang
        wrote on last edited by
        #3

        Thanks. Was just confused since the docs say nothing about "unicast".

        So to set up for unicast UDP, for which I can both send and receive over the same socket, does this look right?

        @void MyUnicastUdpServer::initSocket()
        {
        // initialize UDP for unicast to: 12.34.56.78, port 5678 (bogus IP and port)

         udpSocket = new QUdpSocket(this);
         udpSocket->bind(QHostAddress(12.34.56.78, 5678);
        
         connect(udpSocket, SIGNAL(readyRead()),
                 this, SLOT(readPendingDatagrams()));
        

        }

        void MyUnicastUdpServer::readPendingDatagrams()
        {
        while (udpSocket->hasPendingDatagrams()) {
        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());
        QHostAddress sender;
        quint16 senderPort;

             udpSocket->readDatagram(datagram.data(), datagram.size(),
                                     &sender, &senderPort);
        
             // process The Datagram
        

        }

        void MyUnicastUdpServer::sendDatagram(QByteArray dgram)
        {
        udpSocket->writeDatagram(dgram.data(),dgram.size(), QHostAddress(12.34.56.78, 5678 );
        }
        @

        Thanks.

        1 Reply Last reply
        1
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          I never worked with UDP sockets, but it looks quite ok for me with an exception to the sendDatagram() method. The host address and port used there is that of the host you are sending to (the "remote" host); you give the local address and probably send to yourself :-)

          http://www.catb.org/~esr/faqs/smart-questions.html

          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