Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QT TcpServer detecting input and correctly replying



  • 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


  • Qt Champions 2019

    @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.



  • 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


  • Lifetime Qt Champion

    Hi,

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


  • Qt Champions 2019

    @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



  • @rktech have you checked the Fortune server example?



  • @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.


  • Qt Champions 2019

    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