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. I have doubt on tcp server when multiple client with single server is connected. without multithreading code
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 3.7k 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.
  • B Offline
    B Offline
    bhargav
    wrote on last edited by aha_1980
    #1

    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.

    K mrjjM 2 Replies Last reply
    0
    • B bhargav

      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.

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

      @bhargav

      It is good enough to post in one forum. No sense to copy and paste

      Duplicate of https://forum.qt.io/topic/93943/i-have-doubt-how-to-do-echo-from-server-when-multiple-clients-connect-for-particular-client-reply

      Duplicate of https://forum.qt.io/topic/93942/i-have-doubt-on-tcp-ip-multi-client-with-single-server

      Closing duplications

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

      1 Reply Last reply
      4
      • B bhargav

        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.

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bhargav
          wrote on last edited by VRonin
          #4

          mainwindow.cpp

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          
          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
          
          
              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";
              }
              else
              {
                    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()));
          
          }
          
          MainWindow::~MainWindow()
          {
              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()));
          
          
                 m_pclist->append(server->nextPendingConnection());
          
                 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();
          
              ui->textEdit->setText(buff);
          
          
              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);
          
          
             Sendmessage();
          
          }
          
          void MainWindow::Sendmessage()
          {
          
              QByteArray send;
              QString str = "reply" ;
          
              send.append(str);
          
              sersocket->write(send);
          
              qDebug() <<" sending to client:"<< send;
          }
          
          void MainWindow::delet()
          {
              sersocket->deleteLater();
              exit(0);
          }
          
          1 Reply Last reply
          0
          • B Offline
            B Offline
            bhargav
            wrote on last edited by
            #5

            got the solution thanks for reply...

            aha_1980A 1 Reply Last reply
            1
            • B bhargav

              got the solution thanks for reply...

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @bhargav

              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.

              Thanks!

              Qt has to stay free or it will die.

              1 Reply Last reply
              2

              • Login

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