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. connectToHost for QUdpSocket
Forum Updated to NodeBB v4.3 + New Features

connectToHost for QUdpSocket

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 885 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
    Mark81
    wrote on last edited by
    #1

    I'm working on a class to communicate with a PLC using the Omron FINS (UDP based).
    The low level functions are based on read and write, so according to the docs I have to use connectToHost:

    bool FINS::connectToHost(QHostAddress address, int port)
    {
        _address = address;
        _port = port;
        _socket = new QUdpSocket(this);
    
        _socket->connectToHost(_address, _port);
        return _socket->waitForConnected(1000);
    }
    

    The docs for waitForConnected says:

    Waits until the socket is connected, up to msecs milliseconds. If the connection has been established, this function returns true; otherwise it returns false.

    I wonder how it can work with a connection-less protocol like UDP.
    In fact, it returns true even if the host is offline.

    I need to find a way to understand if the host is online in order to set a "connected" flag to begin the exchange of data.

    JonBJ KroMignonK 2 Replies Last reply
    0
    • M Mark81

      I'm working on a class to communicate with a PLC using the Omron FINS (UDP based).
      The low level functions are based on read and write, so according to the docs I have to use connectToHost:

      bool FINS::connectToHost(QHostAddress address, int port)
      {
          _address = address;
          _port = port;
          _socket = new QUdpSocket(this);
      
          _socket->connectToHost(_address, _port);
          return _socket->waitForConnected(1000);
      }
      

      The docs for waitForConnected says:

      Waits until the socket is connected, up to msecs milliseconds. If the connection has been established, this function returns true; otherwise it returns false.

      I wonder how it can work with a connection-less protocol like UDP.
      In fact, it returns true even if the host is offline.

      I need to find a way to understand if the host is online in order to set a "connected" flag to begin the exchange of data.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Mark81 said in connectToHost for QUdpSocket:

      I need to find a way to understand if the host is online in order to set a "connected" flag to begin the exchange of data.

      Not a UDP expert. But I don't think you can/are supposed to do this with UDP, which as you say is connection-less. Why do you need to do this? I would have thought at best you can send messages and receive messages, if they arrive, so at best you could send a message and see if your host sends a reply which you receive, that's it?

      M 1 Reply Last reply
      0
      • JonBJ JonB

        @Mark81 said in connectToHost for QUdpSocket:

        I need to find a way to understand if the host is online in order to set a "connected" flag to begin the exchange of data.

        Not a UDP expert. But I don't think you can/are supposed to do this with UDP, which as you say is connection-less. Why do you need to do this? I would have thought at best you can send messages and receive messages, if they arrive, so at best you could send a message and see if your host sends a reply which you receive, that's it?

        M Offline
        M Offline
        Mark81
        wrote on last edited by
        #3

        @JonB I could of course. It's not very elegant, indeed.

        Anyway, can you help me to understand this sentence in the QUdpSocket doc page?

        If you want to use the standard QIODevice functions read(), readLine(), write(), etc., you must first connect the socket directly to a peer by calling connectToHost().

        Because there is no "socket conection" in UDP why they say we MUST connect the socket to the peer?

        JonBJ T 2 Replies Last reply
        0
        • M Mark81

          @JonB I could of course. It's not very elegant, indeed.

          Anyway, can you help me to understand this sentence in the QUdpSocket doc page?

          If you want to use the standard QIODevice functions read(), readLine(), write(), etc., you must first connect the socket directly to a peer by calling connectToHost().

          Because there is no "socket conection" in UDP why they say we MUST connect the socket to the peer?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Mark81
          I don't know, but since you are using connectToHost() you are already complying with that statement. Give it a try?

          1 Reply Last reply
          0
          • M Mark81

            @JonB I could of course. It's not very elegant, indeed.

            Anyway, can you help me to understand this sentence in the QUdpSocket doc page?

            If you want to use the standard QIODevice functions read(), readLine(), write(), etc., you must first connect the socket directly to a peer by calling connectToHost().

            Because there is no "socket conection" in UDP why they say we MUST connect the socket to the peer?

            T Offline
            T Offline
            TheLumbee
            wrote on last edited by
            #5

            @Mark81 You can use writeToDatagram() and it will send the packet to the IP/Port without calling connectToHost.

            It's saying if you want to use read(), readLine(), write(), you have to connectToHost() first.

            Instead, just use writeToDatagram() and readFromDatagram().

            1 Reply Last reply
            0
            • ThirdStrandT Offline
              ThirdStrandT Offline
              ThirdStrand
              wrote on last edited by
              #6

              Have a close read of :

              QUdpSocket Documentation in particular the Detailed Description which gives a simple example.

              You need a fundamental understanding of how UDP works. It is really quite different than the TCP Socket. You are not creating a "pipe" between end points, so much as a "loose association".

              1 Reply Last reply
              0
              • M Mark81

                I'm working on a class to communicate with a PLC using the Omron FINS (UDP based).
                The low level functions are based on read and write, so according to the docs I have to use connectToHost:

                bool FINS::connectToHost(QHostAddress address, int port)
                {
                    _address = address;
                    _port = port;
                    _socket = new QUdpSocket(this);
                
                    _socket->connectToHost(_address, _port);
                    return _socket->waitForConnected(1000);
                }
                

                The docs for waitForConnected says:

                Waits until the socket is connected, up to msecs milliseconds. If the connection has been established, this function returns true; otherwise it returns false.

                I wonder how it can work with a connection-less protocol like UDP.
                In fact, it returns true even if the host is offline.

                I need to find a way to understand if the host is online in order to set a "connected" flag to begin the exchange of data.

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #7

                @Mark81 said in connectToHost for QUdpSocket:

                The low level functions are based on read and write, so according to the docs I have to use connectToHost:

                I think you have misunderstood the documentation. There is written:

                The most common way to use this class is to bind to an address and port using bind(), then call writeDatagram() and readDatagram() / receiveDatagram() to transfer data. If you want to use the standard QIODevice functions read(), readLine(), write(), etc., you must first connect the socket directly to a peer by calling connectToHost().

                You need to use connectToHost() to be able to use QIODevice base functions, but this is not mandatory!
                In the case of QUpdSocket, connectToHost() will only record the default endpoint information so QIODevice::write() will be able to send datagram to the right endpoint.

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved