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. Setting UDP output port?

Setting UDP output port?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 8.7k 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.
  • M Offline
    M Offline
    m0khz
    wrote on last edited by
    #1

    New to Qt and enjoying the experience, however....

    I'm having problems with UDP datagrams??? How do I bind to a known outgoing port?

    First I setup a 'discovery receive socket' on port 30303.....

    @
    //setup Discovery UDP socket on port 30303
    discovery_udpSocket = new QUdpSocket(this);
    discovery_udpSocket->bind(QHostAddress::Broadcast,30303);
    connect(discovery_udpSocket, SIGNAL(readyRead()),this, SLOT(processPendingDatagrams()));
    @

    Within the event handler for sending datagram I bind socket again

    @
    // Bind udp socket to Artemis discovery port 30303
    discovery_udpSocket = new QUdpSocket(this);
    //discovery_udpSocket->bind(QHostAddress::Broadcast,30303); // I tried this as well
    discovery_udpSocket->bind(30303);

    // Create test data packet
    // QByteArray datagram = "Message number : " + QByteArray::number(messageNo);
    discovery_udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast,30303);
    

    @

    When I run the application and run WireShark to analyse the datagrams, the outgoing port is being randomly set each time I send a datagram?

    Any help appreciated :)

    Kevin

    [EDIT: code formatting, Volker]

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      Would you mind formatting your code using the proper tags?

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • M Offline
        M Offline
        m0khz
        wrote on last edited by
        #3

        EDIT:
        deleted content as it was duplicate of the OP
        please use the edit function to reformat the code (I've done that for you, this time)
        Volker

        1 Reply Last reply
        0
        • M Offline
          M Offline
          m0khz
          wrote on last edited by
          #4

          The reason for needing to know the source & destination ports is due to the fact
          I'm using the MicroChip TCP/IP stack were the UDPOpen API Syntax is:

          UDP_SOCKET UDPOpen(UDP_PORT localport, NODE_INFO *remoteNode, TCP_PORT remotePort)

          where:

          localPort [in] = Local UDP port number on which data transfer will occure

          remoteNode [in] = Remote host that contains remotePort

          RemoteNode [in] = UDP port number on remote host to transfer the data from.

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

            Have a look at "this forum thread":http://developer.qt.nokia.com/forums/viewthread/4145, they discussed the same topic over there.

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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              m0khz
              wrote on last edited by
              #6

              Thanks for the pointer, from this thread it looks like the send port from within Qt is always random, so it looks like I'll have to write a unique handler for the PIC code...

              Kevin

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dangelog
                wrote on last edited by
                #7

                Did you read the thread? Just bind the socket before sending. That's it.

                Software Engineer
                KDAB (UK) Ltd., a KDAB Group company

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  m0khz
                  wrote on last edited by
                  #8

                  Yes I have read the thread, I am binding the port before sending (see above), the outgoing port is random.
                  It looks like I can call a Berkley API from the PIC, eliminating the need for the Qt send port parameter, I'll experiment tonight.

                  Kevin

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dangelog
                    wrote on last edited by
                    #9

                    bind binds a name to a socket. You're simply binding the wrong name, QHostAddress::Broadcast not being an address of any local interface (are you checking the return value of bind? It's probably failing with EADDRNOTAVAIL). Get a copy of TCP/IP Illustrated and/or Unix Network Programming for the gory details.

                    Software Engineer
                    KDAB (UK) Ltd., a KDAB Group company

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      m0khz
                      wrote on last edited by
                      #10

                      OK that explains it! Thank you, I'll grab a copy of TCP/IP Illustrated, as you suggest, for the 'gory' details :)

                      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