Important: Please read the Qt Code of Conduct -

I have doubt on tcp server when multiple client with single server is connected. without multithreading code

  • My code is working fine ,multi client with single server.
    from client1 is sending "Test1" and server has to echo to that particular client "reply1".
    from client2 is sending "Test2" and server has to echo to that particular client "reply2".

    so ,here I am not getting the logic behind the server code how to get echo for particular client with reply1 ,reply2 ..........reply n.

    In server code without multithreading i am using .
    I have used QList for tcpsocket count and i am not getting how to give reply to particular client.

    Please give suggestions to resolve this task.

  • Lifetime Qt Champion

    Hi maybe show some code ?
    Did you use echo sample or what code do you have now?

  • mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        setWindowTitle("server App");
        server = new QTcpServer;
        sersocket = new QTcpSocket;
        m_pclist = new QList< QTcpSocket *>;
      // m_pcmapper = new QSignalMapper(this);
        if(! server->listen(QHostAddress::Any, 6000))
            qDebug() <<"server not started";
              qDebug() <<"server started";
       // connect(server, SIGNAL(newConnection()), this, SLOT(AcceptConnection()));
          connect(server, SIGNAL(newConnection()), this, SLOT(AccConnection()));
     //   connect(m_pcmapper,SIGNAL(mapped(int)), this, SLOT(Startreading(int)));
          connect(sersocket,SIGNAL(disconnected()),this, SLOT(delet()));
        delete ui;
        delete m_pclist;
    void MainWindow::AccConnection()
    //    m_pclist->append(server->nextPendingConnection());
    //        //here you map each client to its number in the list
    //    m_pcmapper->setMapping(m_pclist->last(), m_pclist->length()-1);
    //        //here we say, that when ever a client from the QList sends readyRead() the mapper should be used
    //        //with the property (list->length()-1) defined in the line above
    //        connect(m_pclist->last(), SIGNAL(readyRead()), m_pcmapper, SLOT( map()));
           connect(m_pclist->last(),SIGNAL(readyRead()),this, SLOT(Startreading()));
    //void MainWindow::AcceptConnection()
    //    sersocket = server->nextPendingConnection();
    //    if(! sersocket)
    //    {
    //        qDebug() <<"connection not accepted";
    //    }
    //    else{
    //         qDebug() <<"connection  accepted";
    //    }
    void MainWindow::Startreading()
        QByteArray buff;
        sersocket = qobject_cast<QTcpSocket*>(sender());
        buff = sersocket->readAll();
        qDebug() <<" received from client:"<< buff;
    //   qDebug() << "Client " << index << " has written: " << m_pclist->at(index)->readAll();
    //    QString strg = m_pclist->at(index)->readAll();
    //    ui->textEdit->setText(strg);
    void MainWindow::Sendmessage()
        QByteArray send;
        QString str = "reply" ;
        qDebug() <<" sending to client:"<< send;
    void MainWindow::delet()

  • got the solution thanks for reply...

  • Lifetime Qt Champion


    1. It is nice to explain your solution here, in case someone has the same problem later.
    2. please close this topic as SOLVED if your issue is solved.


Log in to reply