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. Problem to bind a UDP socket.
Forum Updated to NodeBB v4.3 + New Features

Problem to bind a UDP socket.

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 2.2k 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.
  • J jenya7

    The bind method

    int UDP::Start(QString local_ip, quint16 local_port)
    {
        int result;
    
        //socket->reset();
        //socket->setProxy(QNetworkProxy::NoProxy);
            
        //result = socket->bind(QHostAddress::Any, local_port);
        result = socket->bind(QHostAddress(local_ip), local_port);
    
        QString err = socket->errorString();
    
        result = socket->error();
    
        return result;
    }
    

    In main

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        UDP udp_1 ;
    
        int soc_err;
        QString qs;
    
        QObject::connect(&udp_1, &UDP::ReadyForReader, &reader, &READER::ReadyForReader);
      
        GetNetInfo();
    
        soc_err = udp_1.Start("192.68.3.40", 8001);
    
        if (soc_err)
        {
            qs = QString::number(soc_err);
            cout << "Socket 1 error - " << qs.toStdString() << std::endl;
        }
        else
        {
            cout << "Socket 1 OK\n";
        }
    
        return a.exec();
    }
    

    I see in console
    192.168.3.40
    172.16.1.118
    127.0.0.1
    Socket 1 error - -1

    It sees the IP - 192.168.3.40 but I get an error. Why?

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

    @jenya7 said in Problem to bind a UDP socket.:

    result = socket->bind(QHostAddress(local_ip), local_port);
    QString err = socket->errorString();
    result = socket->error();
    

    This code do not make sense to me, you have to check if operation was not successful before trying to find an error!
    There is no error code "Success" in QAbstractSocket::SocketError (cf. https://doc.qt.io/qt-5/qabstractsocket.html#SocketError-enum).

    Please review your code.

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

    J 1 Reply Last reply
    2
    • J jenya7

      @Christian-Ehrlicher said in Problem to bind a UDP socket.:

      @jenya7 said in Problem to bind a UDP socket.:

      172.16.1.118
      127.0.0.1

      Where do those two IPs come from? Please post your actual code and the real output. Also make sure the IPs you're using are correct (= your host has those IPs assigned)

      172.16.1.118 - is another network card. 127.0.0.1 - default local host.

      I changed for PC with only one network card.
      cmd->ipconfig
      Ethernet adapter Local Area Connection 2:

      Connection-specific DNS Suffix . :
      Link-local IPv6 Address . . . . . : fe80::7d2a:547e:e90a:6e3e%12
      IPv4 Address. . . . . . . . . . . : 172.16.1.139
      Subnet Mask . . . . . . . . . . . : 255.255.255.0
      Default Gateway . . . . . . . . . : 172.16.1.1

      soc_err = udp_1.Start("172.16.1.139", 8001, &err_str);
      

      I get
      172.16.1.139
      127.0.0.1
      Socket 1 error - -1
      Unknown error

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #9

      @jenya7 Please connect a slot to https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred and print the error there.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      0
      • KroMignonK KroMignon

        @jenya7 said in Problem to bind a UDP socket.:

        result = socket->bind(QHostAddress(local_ip), local_port);
        QString err = socket->errorString();
        result = socket->error();
        

        This code do not make sense to me, you have to check if operation was not successful before trying to find an error!
        There is no error code "Success" in QAbstractSocket::SocketError (cf. https://doc.qt.io/qt-5/qabstractsocket.html#SocketError-enum).

        Please review your code.

        J Offline
        J Offline
        jenya7
        wrote on last edited by jenya7
        #10

        @KroMignon said in Problem to bind a UDP socket.:

        @jenya7 said in Problem to bind a UDP socket.:

        result = socket->bind(QHostAddress(local_ip), local_port);
        QString err = socket->errorString();
        result = socket->error();
        

        This code do not make sense to me, you have to check if operation was not successful before trying to find an error!
        There is no error code "Success" in QAbstractSocket::SocketError (cf. https://doc.qt.io/qt-5/qabstractsocket.html#SocketError-enum).

        Please review your code.

        OK.

        int UDP::Start(QString local_ip, quint16 local_port, QString *err_str)
        {
            int result;
        
            //socket->reset();
            //socket->setProxy(QNetworkProxy::NoProxy);
        
            result = socket->bind(QHostAddress::Any, local_port);
            //result = socket->bind(QHostAddress(local_ip), local_port);
        
            if (result != 0)
            {
                *err_str = socket->errorString();
                 result = socket->error();
            }
        
            return result;
        }
        

        So I get result = 1 after result = socket->bind(QHostAddress::Any, local_port);
        Should I trust to a returned result?

        KroMignonK 1 Reply Last reply
        0
        • jsulmJ jsulm

          @jenya7 Please connect a slot to https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred and print the error there.

          J Offline
          J Offline
          jenya7
          wrote on last edited by
          #11

          @jsulm said in Problem to bind a UDP socket.:

          @jenya7 Please connect a slot to https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred and print the error there.

          can not do that.
          it's UDP socket, not Abstract socket. it has no errorOccurred signal.

          QUdpSocket *socket;

          Christian EhrlicherC KroMignonK 2 Replies Last reply
          0
          • J jenya7

            @KroMignon said in Problem to bind a UDP socket.:

            @jenya7 said in Problem to bind a UDP socket.:

            result = socket->bind(QHostAddress(local_ip), local_port);
            QString err = socket->errorString();
            result = socket->error();
            

            This code do not make sense to me, you have to check if operation was not successful before trying to find an error!
            There is no error code "Success" in QAbstractSocket::SocketError (cf. https://doc.qt.io/qt-5/qabstractsocket.html#SocketError-enum).

            Please review your code.

            OK.

            int UDP::Start(QString local_ip, quint16 local_port, QString *err_str)
            {
                int result;
            
                //socket->reset();
                //socket->setProxy(QNetworkProxy::NoProxy);
            
                result = socket->bind(QHostAddress::Any, local_port);
                //result = socket->bind(QHostAddress(local_ip), local_port);
            
                if (result != 0)
                {
                    *err_str = socket->errorString();
                     result = socket->error();
                }
            
                return result;
            }
            

            So I get result = 1 after result = socket->bind(QHostAddress::Any, local_port);
            Should I trust to a returned result?

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

            @jenya7 said in Problem to bind a UDP socket.:

            So I get result = 1 after result = socket->bind(QHostAddress::Any, local_port);
            Should I trust to a returned result?

            Please take time to read a little bit of documentation. This will save you many of your time. It is very easy with Qt Creator, you only have to hit F1 to get help for a function!

            QUdpSocket::bind() (cf. https://doc.qt.io/qt-5/qabstractsocket.html#bind) will return a boolean:

            • true: success
            • false: error

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

            J 1 Reply Last reply
            0
            • J jenya7

              @jsulm said in Problem to bind a UDP socket.:

              @jenya7 Please connect a slot to https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred and print the error there.

              can not do that.
              it's UDP socket, not Abstract socket. it has no errorOccurred signal.

              QUdpSocket *socket;

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #13

              @jenya7 said in Problem to bind a UDP socket.:

              it's UDP socket, not Abstract socket.

              QUdpSocket derives from QAbstractSocket...

              udp_1.Start("192.168.3.40", 8001, &err_str);

              You call Start() with an IP which you computer doesn't have according your ipconfig output above. This can't work. Also 172.16.1.118 is not your IP.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • J jenya7

                @jsulm said in Problem to bind a UDP socket.:

                @jenya7 Please connect a slot to https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred and print the error there.

                can not do that.
                it's UDP socket, not Abstract socket. it has no errorOccurred signal.

                QUdpSocket *socket;

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

                @jenya7 said in Problem to bind a UDP socket.:

                it's UDP socket, not Abstract socket. it has no errorOccurred signal.

                QUpdSocket is based on QAbstractSocket, do you have C++ knowledge?
                Do you know what subclassing means?

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

                J 1 Reply Last reply
                0
                • KroMignonK KroMignon

                  @jenya7 said in Problem to bind a UDP socket.:

                  it's UDP socket, not Abstract socket. it has no errorOccurred signal.

                  QUpdSocket is based on QAbstractSocket, do you have C++ knowledge?
                  Do you know what subclassing means?

                  J Offline
                  J Offline
                  jenya7
                  wrote on last edited by
                  #15

                  @KroMignon said in Problem to bind a UDP socket.:

                  @jenya7 said in Problem to bind a UDP socket.:

                  it's UDP socket, not Abstract socket. it has no errorOccurred signal.

                  QUpdSocket is based on QAbstractSocket, do you have C++ knowledge?
                  Do you know what subclassing means?

                  QObject::connect(socket, &QAbstractSocket::error, this, &UDP::SocketError);
                  
                  void UDP::SocketError(QAbstractSocket::SocketError socketError)
                  {
                      qDebug() << "Socket Error: " << socketError;
                  }
                  

                  I get
                  error: no matching member function for call to 'connect'

                  Christian EhrlicherC KroMignonK 2 Replies Last reply
                  0
                  • J jenya7

                    @KroMignon said in Problem to bind a UDP socket.:

                    @jenya7 said in Problem to bind a UDP socket.:

                    it's UDP socket, not Abstract socket. it has no errorOccurred signal.

                    QUpdSocket is based on QAbstractSocket, do you have C++ knowledge?
                    Do you know what subclassing means?

                    QObject::connect(socket, &QAbstractSocket::error, this, &UDP::SocketError);
                    
                    void UDP::SocketError(QAbstractSocket::SocketError socketError)
                    {
                        qDebug() << "Socket Error: " << socketError;
                    }
                    

                    I get
                    error: no matching member function for call to 'connect'

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #16

                    @jenya7 said in Problem to bind a UDP socket.:

                    QObject::connect(socket, &QAbstractSocket::error, this, &UDP::SocketError);

                    And again to lazy to hit 'F1': https://doc.qt.io/qt-5/qabstractsocket-obsolete.html#error-1

                    Note: Signal error is overloaded in this class. To connect to this signal by using the function pointer syntax, Qt provides a convenient helper for obtaining the function pointer as shown in this example:

                    connect(abstractSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
                        [=](QAbstractSocket::SocketError socketError){ /* ... */ });
                    

                    Also this signal is deprecated - use errorOccurred() if possible (since Qt5.15)

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    2
                    • J jenya7

                      @KroMignon said in Problem to bind a UDP socket.:

                      @jenya7 said in Problem to bind a UDP socket.:

                      it's UDP socket, not Abstract socket. it has no errorOccurred signal.

                      QUpdSocket is based on QAbstractSocket, do you have C++ knowledge?
                      Do you know what subclassing means?

                      QObject::connect(socket, &QAbstractSocket::error, this, &UDP::SocketError);
                      
                      void UDP::SocketError(QAbstractSocket::SocketError socketError)
                      {
                          qDebug() << "Socket Error: " << socketError;
                      }
                      

                      I get
                      error: no matching member function for call to 'connect'

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

                      @jenya7 said in Problem to bind a UDP socket.:

                      QObject::connect(socket, &QAbstractSocket::error, this, &UDP::SocketError);

                      This signal is called errorOccurred() and not error()

                      [EDIT] errorOccurred() has been added with Qt 5.15, if you are using an older Qt version, use code suggested by @Christian-Ehrlicher

                      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
                      • KroMignonK KroMignon

                        @jenya7 said in Problem to bind a UDP socket.:

                        So I get result = 1 after result = socket->bind(QHostAddress::Any, local_port);
                        Should I trust to a returned result?

                        Please take time to read a little bit of documentation. This will save you many of your time. It is very easy with Qt Creator, you only have to hit F1 to get help for a function!

                        QUdpSocket::bind() (cf. https://doc.qt.io/qt-5/qabstractsocket.html#bind) will return a boolean:

                        • true: success
                        • false: error
                        J Offline
                        J Offline
                        jenya7
                        wrote on last edited by
                        #18

                        @KroMignon said in Problem to bind a UDP socket.:

                        @jenya7 said in Problem to bind a UDP socket.:

                        So I get result = 1 after result = socket->bind(QHostAddress::Any, local_port);
                        Should I trust to a returned result?

                        Please take time to read a little bit of documentation. This will save you many of your time. It is very easy with Qt Creator, you only have to hit F1 to get help for a function!

                        QUdpSocket::bind() (cf. https://doc.qt.io/qt-5/qabstractsocket.html#bind) will return a boolean:

                        • true: success
                        • false: error

                        oops. sorry.

                        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