[SOLVED] Problem with qtcpserver socket not triggering event loop slots



  • hello , please help with qtcpserver class , it manages to open socket from incoming connection and also keeps it open if want to send any data from server but wont trigger readyread signals and slots.
    If ready read called mannually it has all data in row that was sent.
    How to make this code correct so signals and slots work.

    It does not have to accept multiple clients , for one class instance 1 client is good.

    ##########################################################
    server.h file

    #ifndef TCP_SERVER_FILE_H
    #define TCP_SERVER_FILE_H

    #include <QObject>
    #include <QTcpServer>
    #include <QTcpSocket>

    class TCP_Server_File : public QObject
    {
    Q_OBJECT
    public:
    explicit TCP_Server_File(QObject *parent = 0);

     void initialize_slots();
    
     void set_adress_and_port_to_listen();
    
     void send_data();
    
     QTcpServer *server;
     QTcpSocket *ConSocket;
    

    public slots:

    //server
    void NewConnection();
    //ConSocket
    void readyReadCon();
    
    void connected();
    void disconnected();
    void bytesWritten(qint64 bytes);
    void connectedstate();
    

    };
    extern TCP_Server_File TCP_COM_Server;

    #endif // TCP_SERVER_FILE_H

    server.cpp file
    #########################################################################

    #include "tcp_server_file.h"

    TCP_Server_File TCP_COM_Server;

    TCP_Server_File::TCP_Server_File(QObject *parent) : QObject(parent)
    {

    }

    void TCP_Server_File::initialize_slots()
    {
    server = new QTcpServer(this);
    ConSocket = new QTcpSocket(this);

    connect(server, SIGNAL(newConnection()),this, SLOT(NewConnection()));
    
    connect(ConSocket, SIGNAL(connected()),this, SLOT(connected()));
    connect(ConSocket, SIGNAL(disconnected()),this, SLOT(disconnected()));
    connect(ConSocket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));
    connect(ConSocket, SIGNAL(readyRead()),this, SLOT(readyReadCon()));
    

    }

    void TCP_Server_File::set_adress_and_port_to_listen()
    {
    server->listen(QHostAddress::Any,4444);
    }

    void TCP_Server_File::send_data()
    {
    ConSocket->write("data sending");
    }

    void TCP_Server_File::NewConnection()
    {

    ConSocket = server->nextPendingConnection();
    ConSocket->write("hello from server");
    

    }

    void TCP_Server_File::readyReadCon()
    {
    qDebug() << "server reading...";

    // read the data from the socket
    qDebug() << ConSocket->readAll();
    

    }

    void TCP_Server_File::connected()
    {
    qDebug() << "server connected...";
    }

    void TCP_Server_File::disconnected()
    {
    qDebug() << "server disconnected...";
    }

    void TCP_Server_File::bytesWritten(qint64 bytes)
    {
    qDebug() << bytes << " server bytes written...";
    }

    void TCP_Server_File::connectedstate()
    {
    qDebug() << "server con state xxx";
    }


  • Moderators

    @Q139
    In NewConnection() you are resetting bascially ConSocket for each new connection request to the server.
    In initialize_slots() you did connect, but those are only valid to the current pointer you have stored in ConSocket. When you assign the sockets those connects are becoming obsolete respectively are are remaining with the original pointer.

    void TCP_Server_File::NewConnection()
    {
    
    ConSocket = server->nextPendingConnection();
    ConSocket->write("hello from server");
    connect(ConSocket, SIGNAL(disconnected()),this, SLOT(disconnected()));
    connect(ConSocket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));
    connect(ConSocket, SIGNAL(readyRead()),this, SLOT(readyReadCon()));
    }
    

    This will probably work in your case, but still has an issue with memory. The previous QTcpSocket stays alive and creates a memory leak.



  • Thanks alot, will try minimalize or remove leak.
    Thanks for good explaining.


  • Moderators

    You are welcome.

    BTW you may remove all ConSocket related stuff from initialize_slots. The sockets will be delivered lateron from QTcpServer.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.