Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QUdpSocket not send but No Error
Forum Updated to NodeBB v4.3 + New Features

QUdpSocket not send but No Error

Scheduled Pinned Locked Moved Solved QML and Qt Quick
16 Posts 5 Posters 2.3k 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.
  • W Offline
    W Offline
    w.tkm
    wrote on last edited by w.tkm
    #1

    I've created a very simple network application.
    I tried to send and receive via UDP, but I can only receive, not send.
    After that, I closed the socket and reopened it and was able to send.
    I checked Tcp/IP and ping, and there were no communication problems with either.

    The problem of not being able to communicate occurs when the application starts, and cannot be fixed without opening and closing the socket.
    I solved the problem by preconfiguring metrics in the network settings.

    Is this a Qt specification? Or is there a problem with the code?

    KroMignonK 1 Reply Last reply
    0
    • W w.tkm

      @KroMignon
      Using writeDatagram, I was able to send it.
      On top of that, there are four things that concern me.

      1. I forgot to remove m_pUdpSendSock->connectToHost(), but I could send.
      2. The return value of m_pudpSendSocket->write() was normal
      3. I actually have another Send socket with the same structure but different IP, port and VLAN, but that one was fine.
      4. When I was using m_pUdpSendSocket->write(), I solved the problem by setting the metric in the network settings.

      I may be confusing you more and more, but what is the cause of all this?

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

      @w-tkm said in QUdpSocket not send but No Error:

      Using writeDatagram, I was able to send it.
      On top of that, there are four things that concern me.

      • I forgot to remove m_pUdpSendSock->connectToHost(), but I could send.

      => is not mandatory, but as written in documentation, writeDatagram() may not work in combination with connectToHost(). So it is better to not use booth with same QUdpStocket.

      • The return value of m_pudpSendSocket->write() was normal
      • I actually have another Send socket with the same structure but different IP, port and VLAN, but that one was fine.
      • When I was using m_pUdpSendSocket->write(), I solved the problem by setting the metric in the network settings.

      I may be confusing you more and more, but what is the cause of all this?

      With m_pUdpSendSocket->writeDatagram() you always specify the destination, so it will always try to resolve destination to get MAC.

      With m_pUdpSendSock->connectToHost() the name resolution is only done once, and store locally. So when result is invalid, no chance to send data to destination!

      It look like your network configuration is not totally clean and the TCP/IP stack can not resolve correctly this destination.

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

      W 1 Reply Last reply
      0
      • 6thC6 Offline
        6thC6 Offline
        6thC
        wrote on last edited by
        #2

        imo: Take a look at the example applications that work.

        https://doc.qt.io/qt-5/qudpsocket.html

        https://doc.qt.io/qt-5/qtnetwork-broadcastsender-example.html
        https://doc.qt.io/qt-5/qtnetwork-broadcastreceiver-example.html
        https://doc.qt.io/qt-5/qtnetwork-multicastsender-example.html
        https://doc.qt.io/qt-5/qtnetwork-multicastreceiver-example.html

        Could be many things: are you sending on the same network interface you receive on etc
        Can you see the TX take place in wireshark etc?

        What code are you using, maybe you're misusing it.
        We need more information as you have a specific issue, we'd need more specifics.

        What I would (and often did) do is go to an example application that works, take a copy and do what I need. If it breaks, copy the example back over and start again or just diff and see where and how you went different|broke things.

        W 1 Reply Last reply
        0
        • W w.tkm

          I've created a very simple network application.
          I tried to send and receive via UDP, but I can only receive, not send.
          After that, I closed the socket and reopened it and was able to send.
          I checked Tcp/IP and ping, and there were no communication problems with either.

          The problem of not being able to communicate occurs when the application starts, and cannot be fixed without opening and closing the socket.
          I solved the problem by preconfiguring metrics in the network settings.

          Is this a Qt specification? Or is there a problem with the code?

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

          @w-tkm said in QUdpSocket not send but No Error:

          Is this a Qt specification? Or is there a problem with the code?

          I am using QUdpSockect for years on Windows/Linux/Android and never had such an issue.
          I believe it is in the way you are doing.
          Hard to say without any 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)

          W 1 Reply Last reply
          0
          • KroMignonK KroMignon

            @w-tkm said in QUdpSocket not send but No Error:

            Is this a Qt specification? Or is there a problem with the code?

            I am using QUdpSockect for years on Windows/Linux/Android and never had such an issue.
            I believe it is in the way you are doing.
            Hard to say without any code.

            W Offline
            W Offline
            w.tkm
            wrote on last edited by w.tkm
            #4

            @KroMignon
            Code is very simple.

            void debugUdp::fnUdpSetting() {
               m_pUdpRecvSock = new QUdpSocket(this);
               m_pUdpSendSock = new QUdpSocket(this);
            
               connect(m_pUdpRecvSock, &QIODevice::readyRead, this, &UsvDebugUdp::fnUdpRecvData);
               m_pUdpRecvSock->bind( QHostAddress( m_strLocalIp ) , m_numConnectPort, QAbstractSocket::DefaultForPlatform); 
            
               m_pUdpSendSock->connectToHost( udpSendIP, udpSendPort);
            }
            
            
            
            KroMignonK 1 Reply Last reply
            0
            • 6thC6 6thC

              imo: Take a look at the example applications that work.

              https://doc.qt.io/qt-5/qudpsocket.html

              https://doc.qt.io/qt-5/qtnetwork-broadcastsender-example.html
              https://doc.qt.io/qt-5/qtnetwork-broadcastreceiver-example.html
              https://doc.qt.io/qt-5/qtnetwork-multicastsender-example.html
              https://doc.qt.io/qt-5/qtnetwork-multicastreceiver-example.html

              Could be many things: are you sending on the same network interface you receive on etc
              Can you see the TX take place in wireshark etc?

              What code are you using, maybe you're misusing it.
              We need more information as you have a specific issue, we'd need more specifics.

              What I would (and often did) do is go to an example application that works, take a copy and do what I need. If it breaks, copy the example back over and start again or just diff and see where and how you went different|broke things.

              W Offline
              W Offline
              w.tkm
              wrote on last edited by
              #5

              @6thC
              I check in wireshark , but no Send.
              Sample Application is all Succsess.

              I don't know difference...

              jsulmJ 1 Reply Last reply
              0
              • W w.tkm

                @6thC
                I check in wireshark , but no Send.
                Sample Application is all Succsess.

                I don't know difference...

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

                @w-tkm said in QUdpSocket not send but No Error:

                but no Send

                You did not post any code where you send anything...

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

                W 1 Reply Last reply
                1
                • W w.tkm

                  @KroMignon
                  Code is very simple.

                  void debugUdp::fnUdpSetting() {
                     m_pUdpRecvSock = new QUdpSocket(this);
                     m_pUdpSendSock = new QUdpSocket(this);
                  
                     connect(m_pUdpRecvSock, &QIODevice::readyRead, this, &UsvDebugUdp::fnUdpRecvData);
                     m_pUdpRecvSock->bind( QHostAddress( m_strLocalIp ) , m_numConnectPort, QAbstractSocket::DefaultForPlatform); 
                  
                     m_pUdpSendSock->connectToHost( udpSendIP, udpSendPort);
                  }
                  
                  
                  
                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #7

                  @w-tkm said in QUdpSocket not send but No Error:

                  m_pUdpSendSock->connectToHost( udpSendIP, udpSendPort);

                  UDP is not an unicast protocol, there is no "connection". connectToHost() is for TCP sockets.

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

                  jeremy_kJ W 2 Replies Last reply
                  0
                  • KroMignonK KroMignon

                    @w-tkm said in QUdpSocket not send but No Error:

                    m_pUdpSendSock->connectToHost( udpSendIP, udpSendPort);

                    UDP is not an unicast protocol, there is no "connection". connectToHost() is for TCP sockets.

                    jeremy_kJ Offline
                    jeremy_kJ Offline
                    jeremy_k
                    wrote on last edited by jeremy_k
                    #8

                    @KroMignon said in QUdpSocket not send but No Error:

                    @w-tkm said in QUdpSocket not send but No Error:

                    m_pUdpSendSock->connectToHost( udpSendIP, udpSendPort);

                    UDP is not an unicast protocol, there is no "connection". connectToHost() is for TCP sockets.

                    Did you mean connected rather than unicast?

                    UDP sockets can be used with connect() in many frameworks to prespecify the remote address. For example:

                    The Connect method establishes a default remote host using the value specified in the endPoint parameter. Once established, you do not have to specify a remote host in each call to the Send method.

                    Asking a question about code? http://eel.is/iso-c++/testcase/

                    1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @w-tkm said in QUdpSocket not send but No Error:

                      but no Send

                      You did not post any code where you send anything...

                      W Offline
                      W Offline
                      w.tkm
                      wrote on last edited by
                      #9

                      @jsulm

                      The part to send is as follows

                      void debugUdp::fnUdpSendtoWifiOrq2(stringstream &sendStream)
                      {
                          quint32 iSendSize;
                          iSendSize = static_cast<quint32>(sendStream.tellp());
                          memcpy(pucSendBuffer, sendStream.str().data(), static_cast<quint32>(iSendSize));
                          m_pUdpSendSocket->write(reinterpret_cast<char*>(pucSendBuffer), static_cast<qint64>(iSendSize));
                      }
                      

                      I don't seem to have a problem here.
                      Because once I disconnect and reconnect, it will start transmitting normally.

                      1 Reply Last reply
                      0
                      • KroMignonK KroMignon

                        @w-tkm said in QUdpSocket not send but No Error:

                        m_pUdpSendSock->connectToHost( udpSendIP, udpSendPort);

                        UDP is not an unicast protocol, there is no "connection". connectToHost() is for TCP sockets.

                        W Offline
                        W Offline
                        w.tkm
                        wrote on last edited by
                        #10

                        @KroMignon

                        As a prerequisite, remember that I can close the socket and reopen it to send.
                        Also, the problem of not being able to communicate only occurs when the application starts, and can be solved by opening and closing the socket.

                        jsulmJ 1 Reply Last reply
                        0
                        • W w.tkm

                          @KroMignon

                          As a prerequisite, remember that I can close the socket and reopen it to send.
                          Also, the problem of not being able to communicate only occurs when the application starts, and can be solved by opening and closing the socket.

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

                          @w-tkm You should add error handling to your app (https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred, https://doc.qt.io/qt-5/qabstractsocket.html#stateChanged).

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

                          W 1 Reply Last reply
                          1
                          • jsulmJ jsulm

                            @w-tkm You should add error handling to your app (https://doc.qt.io/qt-5/qabstractsocket.html#errorOccurred, https://doc.qt.io/qt-5/qabstractsocket.html#stateChanged).

                            W Offline
                            W Offline
                            w.tkm
                            wrote on last edited by
                            #12

                            @jsulm
                            Sorry, but that is what I already did.
                            I didn't get any errors, and there was no problem with the socket status.

                            KroMignonK 1 Reply Last reply
                            0
                            • W w.tkm

                              @jsulm
                              Sorry, but that is what I already did.
                              I didn't get any errors, and there was no problem with the socket status.

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

                              @w-tkm said in QUdpSocket not send but No Error:

                              I didn't get any errors, and there was no problem with the socket status.

                              Did you check the return value of m_pUdpSendSocket->write()?
                              I would suggest you to try with:

                              m_pUdpSendSocket->writeDatagram(reinterpret_cast<char*>(pucSendBuffer), static_cast<qint64>(iSendSize), QHostAddress( m_strLocalIp ) , m_numConnectPort);
                              

                              EDIT: of course before doing this you should remove'm_pUdpSendSock->connectToHost() to avoid errors.

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

                              W 1 Reply Last reply
                              0
                              • KroMignonK KroMignon

                                @w-tkm said in QUdpSocket not send but No Error:

                                I didn't get any errors, and there was no problem with the socket status.

                                Did you check the return value of m_pUdpSendSocket->write()?
                                I would suggest you to try with:

                                m_pUdpSendSocket->writeDatagram(reinterpret_cast<char*>(pucSendBuffer), static_cast<qint64>(iSendSize), QHostAddress( m_strLocalIp ) , m_numConnectPort);
                                

                                EDIT: of course before doing this you should remove'm_pUdpSendSock->connectToHost() to avoid errors.

                                W Offline
                                W Offline
                                w.tkm
                                wrote on last edited by w.tkm
                                #14

                                @KroMignon
                                Using writeDatagram, I was able to send it.
                                On top of that, there are four things that concern me.

                                1. I forgot to remove m_pUdpSendSock->connectToHost(), but I could send.
                                2. The return value of m_pudpSendSocket->write() was normal
                                3. I actually have another Send socket with the same structure but different IP, port and VLAN, but that one was fine.
                                4. When I was using m_pUdpSendSocket->write(), I solved the problem by setting the metric in the network settings.

                                I may be confusing you more and more, but what is the cause of all this?

                                KroMignonK 1 Reply Last reply
                                0
                                • W w.tkm

                                  @KroMignon
                                  Using writeDatagram, I was able to send it.
                                  On top of that, there are four things that concern me.

                                  1. I forgot to remove m_pUdpSendSock->connectToHost(), but I could send.
                                  2. The return value of m_pudpSendSocket->write() was normal
                                  3. I actually have another Send socket with the same structure but different IP, port and VLAN, but that one was fine.
                                  4. When I was using m_pUdpSendSocket->write(), I solved the problem by setting the metric in the network settings.

                                  I may be confusing you more and more, but what is the cause of all this?

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

                                  @w-tkm said in QUdpSocket not send but No Error:

                                  Using writeDatagram, I was able to send it.
                                  On top of that, there are four things that concern me.

                                  • I forgot to remove m_pUdpSendSock->connectToHost(), but I could send.

                                  => is not mandatory, but as written in documentation, writeDatagram() may not work in combination with connectToHost(). So it is better to not use booth with same QUdpStocket.

                                  • The return value of m_pudpSendSocket->write() was normal
                                  • I actually have another Send socket with the same structure but different IP, port and VLAN, but that one was fine.
                                  • When I was using m_pUdpSendSocket->write(), I solved the problem by setting the metric in the network settings.

                                  I may be confusing you more and more, but what is the cause of all this?

                                  With m_pUdpSendSocket->writeDatagram() you always specify the destination, so it will always try to resolve destination to get MAC.

                                  With m_pUdpSendSock->connectToHost() the name resolution is only done once, and store locally. So when result is invalid, no chance to send data to destination!

                                  It look like your network configuration is not totally clean and the TCP/IP stack can not resolve correctly this destination.

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

                                  W 1 Reply Last reply
                                  0
                                  • KroMignonK KroMignon

                                    @w-tkm said in QUdpSocket not send but No Error:

                                    Using writeDatagram, I was able to send it.
                                    On top of that, there are four things that concern me.

                                    • I forgot to remove m_pUdpSendSock->connectToHost(), but I could send.

                                    => is not mandatory, but as written in documentation, writeDatagram() may not work in combination with connectToHost(). So it is better to not use booth with same QUdpStocket.

                                    • The return value of m_pudpSendSocket->write() was normal
                                    • I actually have another Send socket with the same structure but different IP, port and VLAN, but that one was fine.
                                    • When I was using m_pUdpSendSocket->write(), I solved the problem by setting the metric in the network settings.

                                    I may be confusing you more and more, but what is the cause of all this?

                                    With m_pUdpSendSocket->writeDatagram() you always specify the destination, so it will always try to resolve destination to get MAC.

                                    With m_pUdpSendSock->connectToHost() the name resolution is only done once, and store locally. So when result is invalid, no chance to send data to destination!

                                    It look like your network configuration is not totally clean and the TCP/IP stack can not resolve correctly this destination.

                                    W Offline
                                    W Offline
                                    w.tkm
                                    wrote on last edited by
                                    #16

                                    @KroMignon
                                    OK, I think I've figured it out.
                                    I'll look into it some more.
                                    Well, problem solved, thanks.

                                    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