Qt World Summit: Submit your Presentation

readyRead() not triggering

  • I am currently tring to send packets to a server and then receive some. I currently got it so that I send the data and the server then replys but I cant get connect to trigger with readyRead().

    I have wireshark that is catching the send and the servers reply but my program will not get anything.

    void MainWindow::on_ServerListRefreshButton_clicked()
        MyUDP client;
    // myudp.cpp
    #include "myudp.h"
    #include <QDebug>
    MyUDP::MyUDP(QObject *parent) :
        // create a QUDP socket
        socket = new QUdpSocket(this);
        // The most common way to use QUdpSocket class is
        // to bind to an address and port using bind()
        // bool QAbstractSocket::bind(const QHostAddress & address,
        //     quint16 port = 0, BindMode mode = DefaultForPlatform)
        //socket->bind(QHostAddress::LocalHost, 1234);
        connect(socket, SIGNAL(readyRead()), this, SLOT(AreadyRead()));
    void MyUDP::SendRequest(QString ip)
        //Split ip and port
        QString host = ip.left(ip.indexOf(":"));
        int port = 27015;
            port = QString(ip.right(ip.size()-ip.indexOf(":")-1)).toInt();
        //QByteArray Data;
        char* Data = "\xff\xff\xff\xff\x54\x53\x6f\x75\x72\x63\x65\x20\x45\x6e\x67\x69\x6e\x65\x20\x51\x75\x65\x72\x79\x00";
        // Sends the datagram datagram
        // to the host address and at port.
        // qint64 QUdpSocket::writeDatagram(const QByteArray & datagram,
        //                      const QHostAddress & host, quint16 port)
        socket->writeDatagram(Data, 25,QHostAddress(host),port);
    void MyUDP::AreadyRead()
        // when data comes in
        QByteArray buffer;
        QHostAddress sender;
        quint16 senderPort;
        while (socket->hasPendingDatagrams()){
            // qint64 QUdpSocket::readDatagram(char * data, qint64 maxSize,
            //                 QHostAddress * address = 0, quint16 * port = 0)
            // Receives a datagram no larger than maxSize bytes and stores it in data.
            // The sender's host address and port is stored in *address and *port
            // (unless the pointers are 0).
            socket->readDatagram(buffer.data(), buffer.size(),
                                 &sender, &senderPort);
            qDebug() << "Message from: " << sender.toString();
            qDebug() << "Message port: " << senderPort;
            qDebug() << "Message: " << buffer;
    // myudp.h
    #ifndef MYUDP_H
    #define MYUDP_H
    #include <QObject>
    #include <QUdpSocket>
    class MyUDP : public QObject
        explicit MyUDP(QObject *parent = 0);
        void SendRequest(QString ip);
    public slots:
        void AreadyRead();
        QUdpSocket *socket;
    #endif // MYUDP_H

  • @fennec said:

    void MainWindow::on_ServerListRefreshButton_clicked()
    MyUDP client;



    This slot is called, client instantiated. request sent, client is destroyed, cause goes out of scope.
    client is not going to get out of the grave and listen to anything I think,

Log in to reply