How to respond all connected client with an async Qt server



  • 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]


  • Lifetime Qt Champion

    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.



  • 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]


  • Lifetime Qt Champion

    "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


Log in to reply
 

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