Using UDP multi client with single server how to do?
Unsolved
General and Desktop
-
if client 1 : sends "Test1" to server and reply back to client "Reply1".
if client 2 : sends "Test2" to server and reply back to client "Reply2".
if any one know please help me. i have done using TCP but not with UDP.
-
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); setWindowTitle("Server App"); serudpSocket = new QUdpSocket(); serudpSocket->bind(QHostAddress::Any, 6000); connect(serudpSocket, SIGNAL(readyRead()), this, SLOT(readingmsg())); connect(serudpSocket, SIGNAL(disconnected()), this, SLOT(deleteLater())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::readingmsg() { // reading from client QByteArray buffer; buffer.resize(serudpSocket->pendingDatagramSize()); QHostAddress sender; quint16 port; qDebug()<<"port in reading:"<<port; serudpSocket->readDatagram(buffer.data(),buffer.size(),&sender,&port); ui->textEdit->setText(buffer); // Writing to the client,need to specify the client port number. // static int i; QByteArray clientData; QString str = "reply"; // QString index = QString::number(i); clientData.append(str); serudpSocket->writeDatagram(clientData, QHostAddress::LocalHost, port ); // for debugging qDebug()<<"Datagram Recieved From client: "<< buffer.data(); qDebug()<<"Client IP" << sender.toString(); qDebug()<<"port in sending:"<< port; qDebug()<<"Datagram sending to client:"<< clientData.data(); qDebug()<<"\n"; // i++; }
this is my code help me to get desired output?
multi client single server
when client1 sends "test1" it has to send back "reply1"
when client1 sends "test2" it has to send back "reply2" like that ? -
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); setWindowTitle("Server App"); serudpSocket = new QUdpSocket(); serudpSocket->bind(QHostAddress::Any, 6000); connect(serudpSocket, SIGNAL(readyRead()), this, SLOT(readingmsg())); connect(serudpSocket, SIGNAL(disconnected()), this, SLOT(deleteLater())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::readingmsg() { // reading from client QByteArray buffer; buffer.resize(serudpSocket->pendingDatagramSize()); QHostAddress sender; quint16 port; qDebug()<<"port in reading:"<<port; serudpSocket->readDatagram(buffer.data(),buffer.size(),&sender,&port); ui->textEdit->setText(buffer); // Writing to the client,need to specify the client port number. // static int i; QByteArray clientData; QString str = "reply"; // QString index = QString::number(i); clientData.append(str); serudpSocket->writeDatagram(clientData, QHostAddress::LocalHost, port ); // for debugging qDebug()<<"Datagram Recieved From client: "<< buffer.data(); qDebug()<<"Client IP" << sender.toString(); qDebug()<<"port in sending:"<< port; qDebug()<<"Datagram sending to client:"<< clientData.data(); qDebug()<<"\n"; // i++; }
this is my code help me to get desired output?
multi client single server
when client1 sends "test1" it has to send back "reply1"
when client1 sends "test2" it has to send back "reply2" like that ?