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. Specify source UDP port when writing datagram
Forum Update on Monday, May 27th 2025

Specify source UDP port when writing datagram

Scheduled Pinned Locked Moved General and Desktop
19 Posts 6 Posters 25.0k Views
  • 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.
  • D Offline
    D Offline
    dangelog
    wrote on last edited by
    #7

    [quote author="usamytch" date="1298994823"]Unfortunately, I can't send any packet with the code above. When I remove bind from the code, it works well but the source port is random.

    UPDATE: when I wrote like this:

    @send_socket_.bind (60000);
    send_socket_.writeDatagram (datagram, target_address, 7);@

    it works well. But is it legal to omit localhost in bind?[/quote]

    In fact you don't want to bind to localhost, but to QHostAddress::Any or so.

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

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #8

      You send data to a specific port, not from a specific port. The sender port is picked by the OS and it chooses a random, free port for that (the randomness is actually a security feature and not strictly necessary).

      To catch packages you bind to some specific port and listen there for incoming packets.

      I really do not understand why you need the sender port: With UDP it won't set up a connection and you can not send anything back to the sender port anyway!

      If you want to have two parties communicate via UDP, then you need to have both sides listening on a port (each one on a different port of course. The same port number counts as different provided it is on a different host). Then use UDP to send datagrams to the port of the other side.

      1 Reply Last reply
      0
      • U Offline
        U Offline
        usamytch
        wrote on last edited by
        #9

        Thank you for the detailed explanation.

        I need sender port because I communicate with loopback device. When device receive datagram from me, it send me back datagram copy to the same port it received original datagram from. So, I need to fix sender port because I must know what port to listen to receive feedback.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on last edited by
          #10

          loopback device is just a "virtual network card" that connects your host to itself. It does not change the behavior of applications working with it in any unexpected way.

          What you are asking is not possible, are you sure you understood the problem correctly? Is the application you are trying to communicate with actually working? Have you yourself seen any application communicate with it?

          1 Reply Last reply
          0
          • U Offline
            U Offline
            usamytch
            wrote on last edited by
            #11

            Loopback that I meant is not virtual network card, it is real "newtork equipment":http://en.wikipedia.org/wiki/Loopback#Telecommunications

            As I understand you, it's impossible send and receive datagrams from the same UDP socket. So, I'll try to configure loopback to send me feedback to fixed port, other than the port my packets came from. In this case, as you mentioned, I don't need binding.

            As alternative, loopback can send packets to sender_port + n, - in this case I must know sender port and need binding.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tobias.hunger
              wrote on last edited by
              #12

              Are you you need UDP and not TCP?

              This whole setup you describe makes no sense with connectionless UDP but could work well with TCP.

              1 Reply Last reply
              0
              • U Offline
                U Offline
                usamytch
                wrote on last edited by
                #13

                I use UDP, because I implenent software "RFC 2544":http://www.faqs.org/rfc/rfc2544.txt test.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tobias.hunger
                  wrote on last edited by
                  #14

                  I have only glanced over the document: To me the document seems to be using "ports" as in "things you plug network cables into".

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

                    [quote author="Tobias Hunger" date="1299000451"]You send data to a specific port, not from a specific port. The sender port is picked by the OS and it chooses a random, free port for that (the randomness is actually a security feature and not strictly necessary).

                    To catch packages you bind to some specific port and listen there for incoming packets.
                    [/quote]

                    This is wrong. The UNIX way of selecting the source port is always the same: bind(2) a socket with a port. That's exactly what QUdpSocket::bind does in Qt. OP's error was binding on the localhost interface, therefore the packets sent to another host had the random source port.

                    [quote]I need sender port because I communicate with loopback device. When device receive datagram from me, it send me back datagram copy to the same port it received original datagram from. So, I need to fix sender port because I must know what port to listen to receive feedback.[/quote]

                    From this sentence it looks to me that you just need to bind to a (random) source port, which is what every UDP client does in a UDP client/server application. Check out every basic example about UDP, like time/echo servers/clients.

                    The client will usually perform a bind (on a random port), send the request to the server, and the server replies back to the client using the client's source port as its destination one (he knows about the client source port because it gets it in recvfrom() or any similar call).

                    The client may also choose to connect() to the server, in order to use send()/write()/read() instead of sendto()/recvfrom(). On older Unixes it also was the only way to be notified about ICMP errors (not Linux, see the SO_BSDCOMPAT socket opt in socket(7)).

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

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      pala
                      wrote on last edited by
                      #16

                      I have device NetiomUDP, which does the switching of SDI video streams , this device exactly does the same. It receives the commands on UDP packets and returns the UDP packet using the source address. Now in Qt, I am not able to get the source address of the UDP that I sent out. what is the solution.

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        pala
                        wrote on last edited by
                        #17

                        I would like to understand, how do we bind to the random source port, if so how to query to which random source port, the bind function attached the socket in Qt?.

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          capucine
                          wrote on last edited by
                          #18

                          I have the same problem. Did you find a answer?

                          1 Reply Last reply
                          0
                          • C Offline
                            C Offline
                            capucine
                            wrote on last edited by
                            #19

                            I found my problem

                            @ bool bret = m_pcUdpSocket->bind(QHostAddress::Any,m_pcUdpSocket->localPort() ,QUdpSocket::ShareAddress);
                            connect(m_pcUdpSocket, SIGNAL(readyRead()), this, SLOT(SlotReadData()));@@@

                            and

                            @ qint64 nret = m_pcUdpSocket->writeDatagram((const char*)pqMsg->data(),pqMsg->size(),m_cqHostAddress,m_nPortSocket);@

                            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