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. [Solved] Understanding QUdpSocket - deleting
Forum Updated to NodeBB v4.3 + New Features

[Solved] Understanding QUdpSocket - deleting

Scheduled Pinned Locked Moved General and Desktop
13 Posts 4 Posters 6.5k 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.
  • McLionM Offline
    McLionM Offline
    McLion
    wrote on last edited by
    #1

    Hi

    I have some problems understanding how QUdpSocket has to be used.
    My target is to send to a single IP and Port and also receive from the same IP and Port (log server with ack).

    This returns a bind error:
    @if((lola_socket->bind(QHostAddress(ip4addr), port.toUInt(&ok, 10))) != true)
    qDebug() << "bind error for LoLa";
    @

    and this works:
    @ if((lola_socket->bind(QHostAddress::Any, port.toUInt(&ok, 10))) != true)
    qDebug() << "bind error for LoLa";
    @

    For sending: Do I have to give the target address as argument on every send? How can I preset this on the socket?
    For receiving: How do I set the single IP to receive from it (only), or is it usual to listen from any IP on a given port with Any?

    Thanks
    McL

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xander84
      wrote on last edited by
      #2

      Hi,

      first do you have to use UDP for this? To me it sounds like TCP would be better suited for your use case?
      UDP is connection-less, so you don't have to bind to the socket to any IP and port at all, the QUdpSocket class gives you this option only for convenience, but calling "bind" does not actually open a connection and bind to the socket if you use UDP. Same for receiving data, I don't think you can onyl receive data from one IP, the UDP socket receives everything it gets, you can of course ignore every datagram you don't want other than from the one IP.

      Hope that helps you.

      1 Reply Last reply
      0
      • McLionM Offline
        McLionM Offline
        McLion
        wrote on last edited by
        #3

        Hi Xander84,

        Thanks for your inputs and clarification. The server I have to communicate with is nothing that I can influence and it waits for log messages on a specified UDP port and acknowledges the messages to the source on the same port.

        I'll set it up to just listen on the port. If I need to filter the source IP I can still do this with the information returned from QUdpSocket::readDatagram.

        1 Reply Last reply
        0
        • McLionM Offline
          McLionM Offline
          McLion
          wrote on last edited by
          #4

          It works :-)
          There is one thing though: How do I reconfigure, close or terminate a socket bound? If I call it again with a different port for instance, it creates a new socket (netstat -a). Tried closing .. leads to SEGV.

          1 Reply Last reply
          0
          • McLionM Offline
            McLionM Offline
            McLion
            wrote on last edited by
            #5

            Need to kick this up again ... anybody an idea how to close/terminate or delete socket if it's not used anymore?

            Thanks

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jena43
              wrote on last edited by
              #6

              "QUdpSocket destructor":http://qt-project.org/doc/qt-5/qudpsocket.html#dtor.QUdpSocket automaticaly closes the connection.

              bq. QUdpSocket::~QUdpSocket() [virtual]
              Destroys the socket, closing the connection if necessary.

              Have you tried to @delete lola_socket;@

              1 Reply Last reply
              0
              • A Offline
                A Offline
                ambershark
                wrote on last edited by
                #7

                What Jena43 said will do it, and you can specifically do it without destroying your object with:

                @
                lola_socket->disconnectFromHost();
                @

                My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                1 Reply Last reply
                0
                • McLionM Offline
                  McLionM Offline
                  McLion
                  wrote on last edited by
                  #8

                  Yup, works this time :)
                  I already tried delete - to no avail (SIGSEGV) - some time ago. Since you came up with the same suggestion again, I tried again and to my surprise: it works!
                  I dont know what I did wrong last time ...
                  Thanks

                  1 Reply Last reply
                  0
                  • McLionM Offline
                    McLionM Offline
                    McLion
                    wrote on last edited by
                    #9

                    Both seem to work this time.
                    Since my code currently creates a new one if the old one had to be closed:

                    @lola_socket = new QUdpSocket(this);@

                    I think I am gone use

                    @delete lola_socket;@

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      ambershark
                      wrote on last edited by
                      #10

                      As a side note sometimes when I have a crash using delete directly on Qt objects it has to do with outstanding signals that haven't been handled by that object yet.

                      In those situations you can always do

                      @
                      lola_socket->deleteLater()
                      @

                      and the next time the exec loop comes around it will clean up that object once everything it had outstanding has been processed by the event loop.

                      Which is why it works now but didn't before. Different things in the code led to different outstanding signals which you didn't have this time around.

                      Oh, and once you call deleteLater() assume that object and it's pointers are dead and don't use them again, even though they may remain valid for a little bit. After a deleteLater() I tend to set my pointers to 0 just to be sure that if I accidentally use them it's easy to catch.

                      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                      1 Reply Last reply
                      0
                      • McLionM Offline
                        McLionM Offline
                        McLion
                        wrote on last edited by
                        #11

                        What happens if I create a new lola_socket before the old one has been deleted?

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          ambershark
                          wrote on last edited by
                          #12

                          Once you call deleteLater() you can act as if it had been deleted. You can create a new one, even over the same pointer.

                          Even if it was still connected by the time your new socket went to connect the event loop would have cleaned up the old one.

                          So basically, you can think it is deleted just as if you had done delete ptr; on it.

                          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                          1 Reply Last reply
                          0
                          • McLionM Offline
                            McLionM Offline
                            McLion
                            wrote on last edited by
                            #13

                            Thanks a lot for the explanation.

                            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