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 respond all connected client with an async Qt server
QtWS25 Last Chance

How to respond all connected client with an async Qt server

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

    Hi,

    I am very new to Qt programming and just started learning of it. I have developed (by help of various websites) an async client server application. And my server just performing a very simple task by writing a number on client side and also getting a response from server after task performed.
    Method :

    @
    void MyClient::TaskResult(int Number){
    QByteArray Buffer;
    Buffer.append("\r\n Hi Client your task result = ");
    Buffer.append(QString::number(Number));
    socket->write(Buffer);
    }
    @

    Till here everything is fine. But when I am running multiple clients at a time (e.g. sending requests in a loop) I can see only one response from server for one client out of ten .
    Result on client side:
    Connecting...
    Connected!
    Connecting...
    Connected!
    Connecting...
    Connected!
    Connecting...
    Connected!
    Connecting...
    Connected!
    Connecting...
    Connected!
    Connecting...
    Connected!
    Connecting...
    Connected!
    Connecting...
    Connected!
    Connecting...
    Connected!
    we wrote: 9
    we wrote: 9
    we wrote: 9
    we wrote: 9
    we wrote: 9
    we wrote: 9
    we wrote: 9
    we wrote: 9
    we wrote: 9
    we wrote: 9
    Reading...
    ""
    Reading...
    ""
    Reading...
    "
    Hi Client your task result = 100"
    Reading...
    ""
    Reading...
    ""
    Reading...
    ""
    Reading...
    ""
    Reading...
    ""
    Reading...
    ""
    Reading...
    ""
    It should respond for all clients. Isn't it? Please help me to understand it . Don't mind if I am asking a stupid question.

    Thanks .

    [edit: added missing coding tags @ SGaist]

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      With the code you wrote it's difficult to tell. However, have look at Qt's "Fortune Server example":http://qt-project.org/doc/qt-5/qtnetwork-fortuneserver-example.html so see how you can implement that. Compare it with your current implementation.

      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
      0
      • M Offline
        M Offline
        mpattanayak
        wrote on last edited by
        #3

        Hi,

        Thanks for your quick reply. I am just copying some code:
        server.cpp:
        @
        #include "myserver.h"

        MyServer::MyServer(QObject *parent) :
        QTcpServer(parent)
        {
        }

        void MyServer::StartServer()
        {
        if(listen(QHostAddress::Any,1234))
        {
        qDebug() << "started";
        }
        else
        {
        qDebug() << "not started!";
        }
        }

        void MyServer::incomingConnection(int handle)
        {
        MyClient *client = new MyClient(this);
        client->SetSocket(handle);
        clientList.push_back(client);
        }

        void MyServer::SendMessage(QString message)
        {
        int i=0;
        foreach (MyClient *client, clientList) {
        client->TaskResult(i++);
        }
        }
        client.cpp:
        #include "myclient.h"

        MyClient::MyClient(QObject *parent) :
        QObject(parent)
        {
        QThreadPool::globalInstance()->setMaxThreadCount(10);
        }

        void MyClient::SetSocket(int Descriptor)
        {
        while (! socket_list= 0) {
        socket = new QTcpSocket(this);
        connect(socket,SIGNAL(connected()),this,SLOT(connected()));
        connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
        connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));

        socket->setSocketDescriptor(Descriptor);
        
        qDebug() << "client connected" << Descriptor ;
        socket->flush();
        socket_list->append(socket);
        

        }
        }
        void MyClient::connected()
        {
        qDebug() << "client connected event";
        }

        void MyClient::disconnected()
        {
        qDebug() << "client disconnected";
        }
        void MyClient::readyRead()
        {
        qDebug() << socket->readAll();
        MyTask *mytask = new MyTask();
        mytask->setAutoDelete(true);
        connect(mytask,SIGNAL(Result(int)),this,SLOT(TaskResult(int)), Qt::QueuedConnection);
        QThreadPool::globalInstance()->start(mytask);
        }

        void MyClient::TaskResult(int Number)
        {
        for (int i =0; i < 1;i++){
        QByteArray Buffer;
        Buffer.append("\r\nHi Client your Task Result = ");
        Buffer.append(QString::number(Number));
        socket-> write(Buffer);
        socket->write(QString::number(Number).toLatin1());

        }
        }
        @

        It may help you to understand what wrong I am doing.

        Thanks

        [edit: added missing coding tags @ SGaist]

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          "Just copying some code" mixing networking and threading as a beginner is really not a good idea.

          You really should first go read the example I mentioned as well as their non-threaded friends. Start by just the network part to understand what is going on. Only then start studying threads. It's one of the most complicated part to understand and master properly.

          Your code is full of errors and can't work at all as it is.

          It also doesn't make clear what you are trying to achieve

          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
          0

          • Login

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