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. How to receive communication from Multi Client in TcpServer

How to receive communication from Multi Client in TcpServer

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 374 Views 1 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.
  • W Offline
    W Offline
    w.tkm
    wrote on last edited by w.tkm
    #1

    I want to receive communication from Multi Client in TcpServer.
    I was able to connect, but when I did a readyRead, only one of the communications was being received.
    The client will send the communication at about the same time.
    The maximum number of connections is variable.
    Can I receive at the same time without using QThread?

    This is my idea of a connect method.

    class ManagementServerLanTcp : public QObject
    {
        Q_OBJECT
    public:
        ManagementServerLanTcp();
        ~ManagementServerLanTcp();
        void fnTcpServerSet(QString sTcpServerIp, quint16 uiTcpServerPort);
        void fnSocketClose();
    public slots:
        void fnServerConnection();
        void fnTestRead();
    private:
        QTcpServer* m_pManagementServer;
        QTcpSocket* m_pManagementSocket;
        QList<QTcpSocket*> connectList;
    };
    
    ManagementServerLanTcp::ManagementServerLanTcp()
    {
        pucRecvBuffer = new unsigned char[TCP_BUFFSIZE];
        pucSendBuffer = new unsigned char[TCP_BUFFSIZE];
    
        m_pManagementServer = new QTcpServer();
    }
    
    ManagementServerLanTcp::~ManagementServerLanTcp()
    {
        if ( m_pManagementSocket != nullptr ) {
            m_pManagementSocket->close();
            delete  m_pManagementSocket;
        }
        if ( m_pManagementServer != nullptr ) {
            m_pManagementServer->close();
            delete  m_pManagementServer;
        }
    }
    
    void ManagementServerLanTcp::fnTcpServerSet(QString sTcpServerIp, quint16 uiTcpServerPort)
    {
        m_pManagementServer->listen(QHostAddress(sTcpServerIp), uiTcpServerPort);
        connect(m_pManagementServer, SIGNAL(newConnection()), this, SLOT(fnServerConnection()));
    }
    
    
    void ManagementServerLanTcp::fnServerConnection()
    {
        m_pManagementSocket = new QTcpSocket();
        m_pManagementSocket = m_pManagementServer->nextPendingConnection();
        connectList.append(m_pManagementSocket);
    
        int connectListVal = connectList.count() - 1;
        for (int i = 0; i < connectList.count(); i++){
            connect( connectList[connectListVal], SIGNAL(readyRead()), this, SLOT(fnTestRead()));
        }
    }
    
    Pablo J. RoginaP 1 Reply Last reply
    0
    • W w.tkm

      I want to receive communication from Multi Client in TcpServer.
      I was able to connect, but when I did a readyRead, only one of the communications was being received.
      The client will send the communication at about the same time.
      The maximum number of connections is variable.
      Can I receive at the same time without using QThread?

      This is my idea of a connect method.

      class ManagementServerLanTcp : public QObject
      {
          Q_OBJECT
      public:
          ManagementServerLanTcp();
          ~ManagementServerLanTcp();
          void fnTcpServerSet(QString sTcpServerIp, quint16 uiTcpServerPort);
          void fnSocketClose();
      public slots:
          void fnServerConnection();
          void fnTestRead();
      private:
          QTcpServer* m_pManagementServer;
          QTcpSocket* m_pManagementSocket;
          QList<QTcpSocket*> connectList;
      };
      
      ManagementServerLanTcp::ManagementServerLanTcp()
      {
          pucRecvBuffer = new unsigned char[TCP_BUFFSIZE];
          pucSendBuffer = new unsigned char[TCP_BUFFSIZE];
      
          m_pManagementServer = new QTcpServer();
      }
      
      ManagementServerLanTcp::~ManagementServerLanTcp()
      {
          if ( m_pManagementSocket != nullptr ) {
              m_pManagementSocket->close();
              delete  m_pManagementSocket;
          }
          if ( m_pManagementServer != nullptr ) {
              m_pManagementServer->close();
              delete  m_pManagementServer;
          }
      }
      
      void ManagementServerLanTcp::fnTcpServerSet(QString sTcpServerIp, quint16 uiTcpServerPort)
      {
          m_pManagementServer->listen(QHostAddress(sTcpServerIp), uiTcpServerPort);
          connect(m_pManagementServer, SIGNAL(newConnection()), this, SLOT(fnServerConnection()));
      }
      
      
      void ManagementServerLanTcp::fnServerConnection()
      {
          m_pManagementSocket = new QTcpSocket();
          m_pManagementSocket = m_pManagementServer->nextPendingConnection();
          connectList.append(m_pManagementSocket);
      
          int connectListVal = connectList.count() - 1;
          for (int i = 0; i < connectList.count(); i++){
              connect( connectList[connectListVal], SIGNAL(readyRead()), this, SLOT(fnTestRead()));
          }
      }
      
      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #3

      @w-tkm you may want to take a look at Threaded 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

      W 1 Reply Last reply
      1
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #2

        There's no such thing as the "same time", even with threads. You have no control over the arrival of network traffic, its assembly by the operating system, its delivery to your application, or the scheduling of the threads in your application.

        You can, and generally should, service multiple clients without using threads. Given the code I can see, threads will only cause you more problems than any gain you think you might get.

        Here are some general observations:

        • Your constructor does not initialise m_pManagementSocket and destructor assumes it has been. What happens if a connection is never made and you destroy this object?
        • Using fixed size buffers somewhere (unseen). This is a risky choice.
        • Create and immediately orphan a new QTCpSocket on every connection
        • A list of connections with no obvious purpose
        • You connect every QTcpSocket in the list to a slot every time a new connection is made.
        1 Reply Last reply
        3
        • W w.tkm

          I want to receive communication from Multi Client in TcpServer.
          I was able to connect, but when I did a readyRead, only one of the communications was being received.
          The client will send the communication at about the same time.
          The maximum number of connections is variable.
          Can I receive at the same time without using QThread?

          This is my idea of a connect method.

          class ManagementServerLanTcp : public QObject
          {
              Q_OBJECT
          public:
              ManagementServerLanTcp();
              ~ManagementServerLanTcp();
              void fnTcpServerSet(QString sTcpServerIp, quint16 uiTcpServerPort);
              void fnSocketClose();
          public slots:
              void fnServerConnection();
              void fnTestRead();
          private:
              QTcpServer* m_pManagementServer;
              QTcpSocket* m_pManagementSocket;
              QList<QTcpSocket*> connectList;
          };
          
          ManagementServerLanTcp::ManagementServerLanTcp()
          {
              pucRecvBuffer = new unsigned char[TCP_BUFFSIZE];
              pucSendBuffer = new unsigned char[TCP_BUFFSIZE];
          
              m_pManagementServer = new QTcpServer();
          }
          
          ManagementServerLanTcp::~ManagementServerLanTcp()
          {
              if ( m_pManagementSocket != nullptr ) {
                  m_pManagementSocket->close();
                  delete  m_pManagementSocket;
              }
              if ( m_pManagementServer != nullptr ) {
                  m_pManagementServer->close();
                  delete  m_pManagementServer;
              }
          }
          
          void ManagementServerLanTcp::fnTcpServerSet(QString sTcpServerIp, quint16 uiTcpServerPort)
          {
              m_pManagementServer->listen(QHostAddress(sTcpServerIp), uiTcpServerPort);
              connect(m_pManagementServer, SIGNAL(newConnection()), this, SLOT(fnServerConnection()));
          }
          
          
          void ManagementServerLanTcp::fnServerConnection()
          {
              m_pManagementSocket = new QTcpSocket();
              m_pManagementSocket = m_pManagementServer->nextPendingConnection();
              connectList.append(m_pManagementSocket);
          
              int connectListVal = connectList.count() - 1;
              for (int i = 0; i < connectList.count(); i++){
                  connect( connectList[connectListVal], SIGNAL(readyRead()), this, SLOT(fnTestRead()));
              }
          }
          
          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #3

          @w-tkm you may want to take a look at Threaded 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

          W 1 Reply Last reply
          1
          • Pablo J. RoginaP Pablo J. Rogina

            @w-tkm you may want to take a look at Threaded Fortune Server example

            W Offline
            W Offline
            w.tkm
            wrote on last edited by
            #4

            @Pablo-J-Rogina
            OK, Thanks.
            I'll take that as a reference.

            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