QUdpSocket not sending data when created in QThread



  • In a nutshell, how do you send data when a QUdpSocket is created in a QThread?

    Heres my QThread class header:
    @
    #ifndef UDP_H
    #define UDP_H

    #include "defines.h"

    #include <QThread>
    #include <QtNetwork>

    class UDP : public QThread
    {
    Q_OBJECT

    public:
    explicit UDP(QObject *parent = 0);
    void run();
    void commandSend();

    QUdpSocket *udp_socket;
    QByteArray data;
    QString host;
    QString port;
    

    signals:
    void dataReceived(QByteArray);

    private slots:
    void udpSocketReadyRead();

    };

    #endif // UDP_H
    @

    Heres my QThread class cpp:
    @
    #include "udp.h"

    UDP::UDP(QObject *parent) :
    QThread(parent)
    {
    }

    /*
    // main run loop
    /
    void UDP::run()
    {
    /
    object connects */
    udp_socket = new QUdpSocket();
    udp_socket->moveToThread(this);
    connect(udp_socket, SIGNAL(readyRead()), this, SLOT(udpSocketReadyRead()));

    /* start run loop */
    exec&#40;&#41;;
    

    }

    /*
    // send (private function)
    /
    void UDP::commandSend()
    {
    /
    check if the socket is already bound */
    if (udp_socket->state() != QUdpSocket::BoundState){
    // bind the socket
    if (!udp_socket->bind(43680)){
    QMessageBox errMsg;
    errMsg.setWindowTitle("Error");
    errMsg.setText("Error binding socket");
    errMsg.exec();
    return;
    }
    }

    /* send the initiate packet */
    if (udp_socket->writeDatagram(data, QHostAddress(host&#41;, port.toInt(&#41;&#41; < 0&#41;{
        QMessageBox errMsg;
        errMsg.setWindowTitle("UDP Error"&#41;;
        errMsg.setText("Error sending packet"&#41;;
        errMsg.exec(&#41;;
    }
    

    }

    /*
    // udp socket ready read (private slot)
    */
    void UDP::udpSocketReadyRead()
    {

    }
    @

    Heres how i initialize in my main gui thread:
    @
    c_thread->host = IP;
    c_thread->port = PORT;
    ...
    ...
    // fill data buffer goes here
    ...
    ...
    emit commandSend();
    @

    When I call the "commandSend", it does not send the packet... I have watched it on wire shark.

    I know that QThread spawns its own threads to deal with the run loop, but I am having a brain cramp trying to figure out how to successfully get the data sent out the socket. Any ideas?

    Thanks in advance.

    Edit: I have changed a little bit of the code to "moveToThread" to direct the socket connections to the specific thread. Yet, i am still unable to send data...



  • You probably have to call udp_socket->flush() after queuing data with udp_socket->writeDatagram() since these sockets use buffered I/O (and you do not run the exec() event routine of that socket).



  • writeDatagram exits with no errors? And other question, what host ip are you using? If you set host ip the same as local ip you will not see packet in a sniffer. You can check broadcast ip.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.