Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QT TcpServer detecting input and correctly replying

    General and Desktop
    qtcpserver qt5 qtcreator
    4
    8
    707
    Loading More Posts
    • 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.
    • R
      rktech last edited by rktech

      Hi,
      I have a problem. I am trying to implement TcpServer correctly reply on message from qtcpsocket client. But my implementation on ReadyRead don´t work. Can I ask where is problem?
      myserver.cpp

      #include "myserver.h"
      
      MyServer::MyServer(QObject *parent) :
          QObject(parent)
      {
          server = new QTcpServer(this);
      
          connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
      
          if(!server->listen(QHostAddress::Any, 1234))
          {
              qDebug() << "Server could not start!";
          }
          else
          {
              qDebug() << "Server started!";
          }
      }
      
      void MyServer::newConnection()
      {
      
          QTcpSocket *socket = server->nextPendingConnection();
          connect(socket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
      /*
          //socket->write("hello client\r\n");
          //socket->flush();
      
          socket->waitForBytesWritten(3000);
      
          socket->close();*/
      }
      void MyServer::onReadyRead()
      {
          QTcpSocket* sender = static_cast<QTcpSocket*>(QObject::sender());
          QByteArray datas = sender->readAll();
          for (QTcpSocket* socket : _sockets) {
              if (socket != sender){
              QString a = QString::fromStdString(datas.toStdString());
              QString com = a.mid(0,3);
              //41
              //socket->write(QByteArray::fromStdString(/*sender->peerAddress().toString().toStdString() + ": " +*/ a.mid(0,3).toStdString()));
              socket->write("SA");
              socket->flush();
              socket->waitForBytesWritten(5000);
              socket->close();
          }
      }
      }
      

      myserver.h

      #ifndef MYSERVER_H
      #define MYSERVER_H
      
      #include <QObject>
      #include <QDebug>
      #include <QTcpServer>
      #include <QTcpSocket>
      #include <QString>
      class MyServer : public QObject
      {
          Q_OBJECT
      public:
          explicit MyServer(QObject *parent = 0);
          
      signals:
          
      public slots:
          void newConnection();
          void onReadyRead();
      
      private:
          QTcpServer *server;
          QList<QTcpSocket*>  _sockets;
          QString a;
          QString com;
          QTcpSocket *socket;
      };
      
      #endif // MYSERVER_H
      

      main.cpp

      #include "mainwindow.h"
      #include <QApplication>
      #include "myserver.h"
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          MyServer S;
          return a.exec();
      }
      

      mainwindow.cpp and mainwindow.h is not edited.
      Thanks
      rktech

      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        @rktech said in QT TcpServer detecting input and correctly replying:

        But my implementation on ReadyRead don´t work.

        What exactly don't work?

        for (QTcpSocket* socket : _sockets) {

        Why do you do this? Just dynamic_cast the sender, check for != nullptr and use this QTcpSocket.

        Qt has to stay free or it will die.

        1 Reply Last reply Reply Quote 1
        • R
          rktech last edited by

          My client didn´t have any response. Can you fix my whole code? I don´t know what do you mean by nullptr.
          thanks
          rktech

          Pablo J. Rogina 1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Hi,

            Based on your code, _sockets is never modified, neither on a new connection nor when a connection is closed.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 1
            • Christian Ehrlicher
              Christian Ehrlicher Lifetime Qt Champion last edited by

              @rktech said in QT TcpServer detecting input and correctly replying:

              Can you fix my whole code?

              I'm not here to fix code for anyone, just for helping.

              First I would not directly close the connection and don't use waitForBytesWritten() but signals/slots.

              I don´t know what do you mean by nullptr.

              See what dynamic_cast is doing: https://en.cppreference.com/w/cpp/language/dynamic_cast or qobject_cast which does work here too: https://doc.qt.io/qt-5/qobject.html#qobject_cast

              Qt has to stay free or it will die.

              1 Reply Last reply Reply Quote 2
              • Pablo J. Rogina
                Pablo J. Rogina @rktech last edited by

                @rktech have you checked the Fortune server example?

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                R 1 Reply Last reply Reply Quote 1
                • R
                  rktech @Pablo J. Rogina last edited by

                  @pablo-j-rogina there is nothing about reading input from client.
                  @Christian-Ehrlicher I read that, but i don´t know what i am doing wrong.

                  1 Reply Last reply Reply Quote 0
                  • Christian Ehrlicher
                    Christian Ehrlicher Lifetime Qt Champion last edited by

                    As @SGaist pointed out you don't fill _sockets anywhere and as I pointed out you don't need to search the socket in _sockets at all since you already cast QObject::sender() to the correct QTcpSocket although qobject_cast or dynamic_cast should be used instead static_cast

                    Qt has to stay free or it will die.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post