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 issue
Qt 6.11 is out! See what's new in the release blog

QUdpSocket issue

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.1k 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    Hi,
    I have a simple udp socket server. It recives what hosts write on, but it can't answer them. i get QIODevice::write (QUdpSocket): device not open when i try to call write(). Can please someone tell me what is missing ?

    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
    
        QUdpSocket *udpSrv = new QUdpSocket();
        udpSrv->bind(QHostAddress::LocalHost, 1234);
    
        QObject::connect(udpSrv,&QUdpSocket:: bytesWritten,[&](){
            qDebug()<<"Server answered !"; //
        });
    
        QObject::connect(udpSrv, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),[&]() {
            qDebug()<< "Error : " << udpSrv->errorString();
        });
    
        QObject::connect(udpSrv,&QUdpSocket::readyRead,[&](){
            QByteArray buffer;
            QHostAddress sender;
            quint16 senderPort;
            buffer.resize(static_cast<int>(udpSrv->pendingDatagramSize()));
    
            udpSrv->readDatagram(buffer.data(), buffer.size(),
                                 &sender, &senderPort);
    
            QString req = QString::fromStdString(buffer.toStdString());
    
            qDebug()<<"client : " << req;
    
            if(req=="Hi"){
                udpSrv->write("Hello!"); // QIODevice::write (QUdpSocket): device not open
            }
            else{
                udpSrv->write("Say Hello First !"); // QIODevice::write (QUdpSocket): device not open
            }
        });
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    
    raven-worxR 1 Reply Last reply
    0
    • ODБOïO ODБOï

      Hi,
      I have a simple udp socket server. It recives what hosts write on, but it can't answer them. i get QIODevice::write (QUdpSocket): device not open when i try to call write(). Can please someone tell me what is missing ?

      int main(int argc, char *argv[])
      {
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
          QGuiApplication app(argc, argv);
      
          QUdpSocket *udpSrv = new QUdpSocket();
          udpSrv->bind(QHostAddress::LocalHost, 1234);
      
          QObject::connect(udpSrv,&QUdpSocket:: bytesWritten,[&](){
              qDebug()<<"Server answered !"; //
          });
      
          QObject::connect(udpSrv, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),[&]() {
              qDebug()<< "Error : " << udpSrv->errorString();
          });
      
          QObject::connect(udpSrv,&QUdpSocket::readyRead,[&](){
              QByteArray buffer;
              QHostAddress sender;
              quint16 senderPort;
              buffer.resize(static_cast<int>(udpSrv->pendingDatagramSize()));
      
              udpSrv->readDatagram(buffer.data(), buffer.size(),
                                   &sender, &senderPort);
      
              QString req = QString::fromStdString(buffer.toStdString());
      
              qDebug()<<"client : " << req;
      
              if(req=="Hi"){
                  udpSrv->write("Hello!"); // QIODevice::write (QUdpSocket): device not open
              }
              else{
                  udpSrv->write("Say Hello First !"); // QIODevice::write (QUdpSocket): device not open
              }
          });
      
          QQmlApplicationEngine engine;
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          if (engine.rootObjects().isEmpty())
              return -1;
      
          return app.exec();
      }
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @LeLev
      you should rather use QUdpSocket::writeDatagram()
      but first before anything else you should read the docs and/or at least one of the official examples...

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      ODБOïO 2 Replies Last reply
      2
      • raven-worxR raven-worx

        @LeLev
        you should rather use QUdpSocket::writeDatagram()
        but first before anything else you should read the docs and/or at least one of the official examples...

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        @raven-worx said in QUdpSocket issue:

        should

        my bad
        thank you

        1 Reply Last reply
        0
        • raven-worxR raven-worx

          @LeLev
          you should rather use QUdpSocket::writeDatagram()
          but first before anything else you should read the docs and/or at least one of the official examples...

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by ODБOï
          #4

          @raven-worx
          now i'm able to write to the socket, but the tool i use as client (Packet Sender) does not receive anything, do you have an idea why ?

                      QString resp = "Hello client !";
                      qint64 nwritten = udpSrv->writeDatagram(resp.toLatin1(),resp.length(),QHostAddress::LocalHost,1234);
                      qDebug()<< "wrote : " << nwritten; // output 14
          

          edit

          i fixed, last parameter must be senderPort instead of 1234
          writeDatagram(resp.toLatin1(),resp.length(),QHostAddress::LocalHost,senderPort)

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Cabrera1M
            Banned
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            -1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved