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. Using UDP multi client with single server how to do?
Forum Updated to NodeBB v4.3 + New Features

Using UDP multi client with single server how to do?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.6k 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 24 Aug 2018, 08:32 last edited by
    #1

    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.

    J 1 Reply Last reply 24 Aug 2018, 12:53
    0
    • V Offline
      V Offline
      VRonin
      wrote on 24 Aug 2018, 08:56 last edited by
      #2

      QTcpSocket and QUdpSocket are both QIODevices so you can read/write them in the exact same way

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3
      • B bhargav
        24 Aug 2018, 08:32

        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.

        J Offline
        J Offline
        JonB
        wrote on 24 Aug 2018, 12:53 last edited by
        #3

        @bhargav
        Can you even do this with UDP?? "Send replies back"? It makes no sense. For one thing, the client may (well) not even receive the datagram...

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bhargav
          wrote on 25 Aug 2018, 04:10 last edited by VRonin
          #4
          #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 ?

          V 1 Reply Last reply 25 Aug 2018, 05:52
          0
          • B bhargav
            25 Aug 2018, 04:10
            #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 ?

            V Offline
            V Offline
            VRonin
            wrote on 25 Aug 2018, 05:52 last edited by
            #5

            @bhargav said in Using UDP multi client with single server how to do?:

            serudpSocket->writeDatagram(clientData, QHostAddress::LocalHost, port );

            you are sending to QHostAddress::LocalHost instead of sender

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1
            • B Offline
              B Offline
              bhargav
              wrote on 25 Aug 2018, 06:32 last edited by
              #6

              Here i am testing in single computer. so i thought local host means 127.0.0.1?

              1 Reply Last reply
              0

              1/6

              24 Aug 2018, 08:32

              • Login

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