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. QT TcpServer detecting input and correctly replying
QtWS25 Last Chance

QT TcpServer detecting input and correctly replying

Scheduled Pinned Locked Moved Solved General and Desktop
qtcpserverqt5qtcreator
8 Posts 4 Posters 1.2k Views
  • 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 Offline
    R Offline
    rktech
    wrote on 7 Sept 2019, 16:23 last edited by rktech 9 Jul 2019, 16:25
    #1

    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
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 7 Sept 2019, 16:36 last edited by
      #2

      @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • R Offline
        R Offline
        rktech
        wrote on 7 Sept 2019, 16:40 last edited by
        #3

        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

        P 1 Reply Last reply 7 Sept 2019, 18:51
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 7 Sept 2019, 18:23 last edited by
          #4

          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
          1
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 7 Sept 2019, 18:23 last edited by
            #5

            @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            2
            • R rktech
              7 Sept 2019, 16:40

              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

              P Offline
              P Offline
              Pablo J. Rogina
              wrote on 7 Sept 2019, 18:51 last edited by
              #6

              @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 7 Sept 2019, 20:25
              1
              • P Pablo J. Rogina
                7 Sept 2019, 18:51

                @rktech have you checked the Fortune server example?

                R Offline
                R Offline
                rktech
                wrote on 7 Sept 2019, 20:25 last edited by
                #7

                @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
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 8 Sept 2019, 07:04 last edited by
                  #8

                  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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0

                  1/8

                  7 Sept 2019, 16:23

                  • Login

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