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 not sending data when created in QThread

QUdpSocket not sending data when created in QThread

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.6k 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.
  • V Offline
    V Offline
    vezprog
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • F Offline
      F Offline
      franku
      wrote on last edited by
      #2

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

      This, Jen, is the internet.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nibbit
        wrote on last edited by
        #3

        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.

        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