Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QUdpSocket not send but No Error



  • 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?



  • @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.



  • 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-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.



  • @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);
    }
    
    
    


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

    I don't know difference...


  • Lifetime Qt Champion

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

    but no Send

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



  • @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.



  • @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.



  • @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.



  • @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.


  • Lifetime Qt Champion



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



  • @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.



  • @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?



  • @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.



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


Log in to reply