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. [SOLVED] Problem with qtcpserver socket not triggering event loop slots
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.5k Views 2 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.
  • Q Offline
    Q Offline
    Q139
    wrote on last edited by Q139
    #1

    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";
    }

    K 1 Reply Last reply
    0
    • Q Q139

      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";
      }

      K Offline
      K Offline
      koahnig
      wrote on last edited by koahnig
      #2

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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      1
      • Q Offline
        Q Offline
        Q139
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          You are welcome.

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

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0

          • Login

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