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. readyRead() not triggering
Qt 6.11 is out! See what's new in the release blog

readyRead() not triggering

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.2k 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.
  • F Offline
    F Offline
    fennec
    wrote on last edited by
    #1

    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.

    //main.cpp
    void MainWindow::on_ServerListRefreshButton_clicked()
    {
        MyUDP client;
    
        client.SendRequest("193.192.58.145:27315");
    }
    
    // myudp.cpp
    
    #include "myudp.h"
    #include <QDebug>
    
    MyUDP::MyUDP(QObject *parent) :
        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);
        socket->bind(QHostAddress("193.192.58.145"),27315);
    
        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;
        if(ip.contains(":"))
            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()){
            buffer.fill(0,socket->pendingDatagramSize());
    
            // 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
    {
        Q_OBJECT
    public:
        explicit MyUDP(QObject *parent = 0);
        void SendRequest(QString ip);
    signals:
    
    public slots:
        void AreadyRead();
    
    private:
        QUdpSocket *socket;
    
    };
    
    #endif // MYUDP_H
    
    1 Reply Last reply
    0
    • A Offline
      A Offline
      alex_malyu
      wrote on last edited by
      #2

      @fennec said:

      //main.cpp
      void MainWindow::on_ServerListRefreshButton_clicked()
      {
      MyUDP client;

      client.SendRequest("193.192.58.145:27315");
      

      }

      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,

      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