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

QUdpSocket bind problem

Scheduled Pinned Locked Moved Solved General and Desktop
27 Posts 5 Posters 548 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.
  • J Offline
    J Offline
    JulsPower
    wrote last edited by
    #5

    Hi
    im using qt creator 16 under windows 11, im pretty sure the debugger is GDB under the hood

    JonBJ 1 Reply Last reply
    0
    • J JulsPower

      Hi
      im using qt creator 16 under windows 11, im pretty sure the debugger is GDB under the hood

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote last edited by
      #6
      This post is deleted!
      1 Reply Last reply
      0
      • J JulsPower

        Hi
        got a small problem with the bind function

            m_UdpSocket = new QUdpSocket();
            bool b;
            while(m_UdpSocket->state() != QAbstractSocket::BoundState)
                b = m_UdpSocket->bind(QHostAddress::Any, 55555);
        

        while debugging it always return false and stays in the loop
        in release mode work on first try and the socket is correctly binded and receive datagram send on that port
        any idea why?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote last edited by
        #7

        @JulsPower You are blocking the Qt event loop with such a loop, not sure that is the issue here

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

        1 Reply Last reply
        0
        • HansonH Offline
          HansonH Offline
          Hanson
          wrote last edited by
          #8

          @JulsPower Hi
          Maybe you can check what the error value for m_UdpSocket->state() means first.
          https://doc.qt.io/qt-6/qabstractsocket.html#SocketState-enum

          “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
          —— Martin Golding

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JulsPower
            wrote last edited by
            #9

            Ok I changed the init function to be more like it should be

                ui->setupUi(this);
                m_UdpSocket = new QUdpSocket(this);
                bool b;
                b = m_UdpSocket->bind(QHostAddress::Any, 55555);
                auto state = m_UdpSocket->state();
                QMessageBox::information(this, "info", QString(b?"true ":"false ") + QString::number(state));
                connect(m_UdpSocket, &QUdpSocket::readyRead, this, &MainWindow::readPendingDatagrams);
                connect(m_UdpSocket, &QUdpSocket::errorOccurred, this, &MainWindow::errorOccurred);
            
            

            if I debug the message box show false 0 (which is unconnected)
            and if I run the debug version it shows true 4

            errorOccurred is never called

            HansonH 1 Reply Last reply
            0
            • J JulsPower

              Ok I changed the init function to be more like it should be

                  ui->setupUi(this);
                  m_UdpSocket = new QUdpSocket(this);
                  bool b;
                  b = m_UdpSocket->bind(QHostAddress::Any, 55555);
                  auto state = m_UdpSocket->state();
                  QMessageBox::information(this, "info", QString(b?"true ":"false ") + QString::number(state));
                  connect(m_UdpSocket, &QUdpSocket::readyRead, this, &MainWindow::readPendingDatagrams);
                  connect(m_UdpSocket, &QUdpSocket::errorOccurred, this, &MainWindow::errorOccurred);
              
              

              if I debug the message box show false 0 (which is unconnected)
              and if I run the debug version it shows true 4

              errorOccurred is never called

              HansonH Offline
              HansonH Offline
              Hanson
              wrote last edited by
              #10

              @JulsPower
              You can now use error() or errorString() to see the actual error message.
              https://doc.qt.io/qt-6/qabstractsocket.html#error
              https://doc.qt.io/qt-6/qiodevice.html#errorString

              “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
              —— Martin Golding

              1 Reply Last reply
              0
              • J Offline
                J Offline
                JulsPower
                wrote last edited by
                #11

                indeed
                QMessageBox::information(this, "info", QString(b?"true ":"false ") + QString::number(state) + m_UdpSocket->errorString());
                return unknow error :(

                1 Reply Last reply
                0
                • HansonH Offline
                  HansonH Offline
                  Hanson
                  wrote last edited by
                  #12

                  @JulsPower What is return value of error().

                  “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
                  —— Martin Golding

                  J 1 Reply Last reply
                  0
                  • HansonH Hanson

                    @JulsPower What is return value of error().

                    J Offline
                    J Offline
                    JulsPower
                    wrote last edited by
                    #13

                    @Hanson said in QUdpSocket bind problem:

                    @JulsPower What is return value of error().

                    -1

                    1 Reply Last reply
                    0
                    • HansonH Offline
                      HansonH Offline
                      Hanson
                      wrote last edited by
                      #14

                      @JulsPower
                      I checked the relevant source code and I can see that the error code without an error message could be WSANOTINITIALISED, which stands for WSAStartup failed. I guess it could be a mismatch or missing winsock library in the debug environment.
                      https://learn.microsoft.com/zh-cn/windows/win32/winsock/windows-sockets-error-codes-2

                      bool QNativeSocketEnginePrivate::nativeBind(const QHostAddress &a, quint16 port)
                      {
                          QHostAddress address = a;
                          if (address.protocol() == QAbstractSocket::IPv4Protocol) {
                              if ((address.toIPv4Address() & 0xffff0000) == 0xefff0000) {
                                  // binding to a multicast address
                                  address = QHostAddress(QHostAddress::AnyIPv4);
                              }
                          }
                      
                          qt_sockaddr aa;
                          QT_SOCKLEN_T sockAddrSize = 0;
                          setPortAndAddress(port, address, &aa, &sockAddrSize);
                      
                          if (aa.a.sa_family == AF_INET6) {
                              // The default may change in future, so set it explicitly
                              int ipv6only = 0;
                              if (address.protocol() == QAbstractSocket::IPv6Protocol)
                                  ipv6only = 1;
                              ::setsockopt(socketDescriptor, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&ipv6only, sizeof(ipv6only) );
                          }
                      
                      
                          int bindResult = ::bind(socketDescriptor, &aa.a, sockAddrSize);
                          if (bindResult == SOCKET_ERROR && WSAGetLastError() == WSAEAFNOSUPPORT
                                  && address.protocol() == QAbstractSocket::AnyIPProtocol) {
                              // retry with v4
                              aa.a4.sin_family = AF_INET;
                              aa.a4.sin_port = htons(port);
                              aa.a4.sin_addr.s_addr = htonl(address.toIPv4Address());
                              sockAddrSize = sizeof(aa.a4);
                              bindResult = ::bind(socketDescriptor, &aa.a, sockAddrSize);
                          }
                          if (bindResult == SOCKET_ERROR) {
                              int err = WSAGetLastError();
                              WS_ERROR_DEBUG(err);
                              switch (err) {
                              case WSANOTINITIALISED:
                                  //###
                                  break;
                              case WSAEADDRINUSE:
                              case WSAEINVAL:
                                  setError(QAbstractSocket::AddressInUseError, AddressInuseErrorString);
                                  break;
                              case WSAEACCES:
                                  setError(QAbstractSocket::SocketAccessError, AddressProtectedErrorString);
                                  break;
                              case WSAEADDRNOTAVAIL:
                                  setError(QAbstractSocket::SocketAddressNotAvailableError, AddressNotAvailableErrorString);
                                  break;
                              default:
                                  break;
                              }
                      
                      #if defined (QNATIVESOCKETENGINE_DEBUG)
                              qDebug("QNativeSocketEnginePrivate::nativeBind(%s, %i) == false (%s)",
                                     address.toString().toLatin1().constData(), port, socketErrorString.toLatin1().constData());
                      #endif
                      
                              return false;
                          }
                      
                      #if defined (QNATIVESOCKETENGINE_DEBUG)
                          qDebug("QNativeSocketEnginePrivate::nativeBind(%s, %i) == true",
                                 address.toString().toLatin1().constData(), port);
                      #endif
                          socketState = QAbstractSocket::BoundState;
                          return true;
                      }
                      

                      “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
                      —— Martin Golding

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        JulsPower
                        wrote last edited by
                        #15

                        Interesting
                        would you know a way to confirm this? and even more fix it?

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          JulsPower
                          wrote last edited by
                          #16

                          I did update to qt creator 17 with all debug symbol, issue remain

                          1 Reply Last reply
                          0
                          • HansonH Offline
                            HansonH Offline
                            Hanson
                            wrote last edited by Hanson
                            #17

                            @JulsPower
                            You can try the following code before executing the bind() function

                            #include <winsock2.h>
                            #include <ws2tcpip.h>
                            #pragma comment(lib, "ws2_32.lib")
                            
                            ....
                            WSAData wsadata;
                            int err = WSAStartup(MAKEWORD(2,0), &wsadata);
                            if (err != 0) {
                                    qWarning("QTcpSocketAPI: WinSock v2.0 initialization failed.");
                                } 
                            ....
                            

                            “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
                            —— Martin Golding

                            J 1 Reply Last reply
                            0
                            • HansonH Hanson

                              @JulsPower
                              You can try the following code before executing the bind() function

                              #include <winsock2.h>
                              #include <ws2tcpip.h>
                              #pragma comment(lib, "ws2_32.lib")
                              
                              ....
                              WSAData wsadata;
                              int err = WSAStartup(MAKEWORD(2,0), &wsadata);
                              if (err != 0) {
                                      qWarning("QTcpSocketAPI: WinSock v2.0 initialization failed.");
                                  } 
                              ....
                              
                              J Offline
                              J Offline
                              JulsPower
                              wrote last edited by
                              #18

                              @Hanson said in QUdpSocket bind problem:

                              @JulsPower
                              You can try the following code before executing the bind() function

                              #include <winsock2.h>
                              #include <ws2tcpip.h>
                              #pragma comment(lib, "ws2_32.lib")
                              
                              ....
                              WSAData wsadata;
                              int err = WSAStartup(MAKEWORD(2,0), &wsadata);
                              if (err != 0) {
                                      qWarning("QTcpSocketAPI: WinSock v2.0 initialization failed.");
                                  } 
                              ....
                              

                              WSAStartup return 0 so no error there
                              but the bind function continue to return false

                              1 Reply Last reply
                              0
                              • HansonH Offline
                                HansonH Offline
                                Hanson
                                wrote last edited by
                                #19

                                @JulsPower
                                emmm... I can't figure it out anymore :)
                                Or you can download the qt source code and debug it.

                                “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
                                —— Martin Golding

                                1 Reply Last reply
                                0
                                • J Offline
                                  J Offline
                                  JulsPower
                                  wrote last edited by
                                  #20

                                  WSAGetLastError return error 10106 (WSAEPROVIDERFAILEDINIT)
                                  gives you an idea??

                                  HansonH 1 Reply Last reply
                                  0
                                  • J JulsPower

                                    WSAGetLastError return error 10106 (WSAEPROVIDERFAILEDINIT)
                                    gives you an idea??

                                    HansonH Offline
                                    HansonH Offline
                                    Hanson
                                    wrote last edited by
                                    #21

                                    @JulsPower
                                    Can you show me where in your code calling WSAGetLastError()?

                                    “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
                                    —— Martin Golding

                                    J 1 Reply Last reply
                                    0
                                    • HansonH Hanson

                                      @JulsPower
                                      Can you show me where in your code calling WSAGetLastError()?

                                      J Offline
                                      J Offline
                                      JulsPower
                                      wrote last edited by
                                      #22

                                      @Hanson said in QUdpSocket bind problem:

                                      @JulsPower
                                      Can you show me where in your code calling WSAGetLastError()?

                                      b = m_UdpSocket->bind(QHostAddress::Any, 55555);
                                      auto state = m_UdpSocket->state();
                                      QMessageBox::information(this, "info", QString(b?"true ":"false ") + QString::number(state) + QString::number(WSAGetLastError()));
                                      

                                      show while debugging it shows false 010106

                                      1 Reply Last reply
                                      0
                                      • J Offline
                                        J Offline
                                        JulsPower
                                        wrote last edited by JulsPower
                                        #23

                                        I replicated the samething in a new project using qmake instead of cmake. lo and behold it works in debugging mode...
                                        seem ill be using qmake

                                        HansonH 1 Reply Last reply
                                        0
                                        • J JulsPower

                                          I replicated the samething in a new project using qmake instead of cmake. lo and behold it works in debugging mode...
                                          seem ill be using qmake

                                          HansonH Offline
                                          HansonH Offline
                                          Hanson
                                          wrote last edited by Hanson
                                          #24

                                          @JulsPower
                                          Maybe same as this question
                                          https://stackoverflow.com/questions/48597996/qtcpserver-return-unknown-error-on-listen-function-only-under-debugger
                                          https://developercommunity.visualstudio.com/t/native-cmake-support-application-debug-launch-is-b/562174

                                          So the reason could be that when debugging the program with cmake project, the environment variables were cleared and the SystemRoot variable was missing, which caused WSASocket to fail to load the related components.

                                          “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
                                          —— Martin Golding

                                          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