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.
-
It is good enough to post in one forum. No sense to copy and paste
Duplicate of https://forum.qt.io/topic/93942/i-have-doubt-on-tcp-ip-multi-client-with-single-server
Closing duplications
-
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); }