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. How to Share Any File Using QUdpSocket
Forum Updated to NodeBB v4.3 + New Features

How to Share Any File Using QUdpSocket

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 582 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.
  • Ketan__Patel__0011K Offline
    Ketan__Patel__0011K Offline
    Ketan__Patel__0011
    wrote on last edited by
    #1

    Hello Experts

    I Want To Share Any File Like (Image File , etc..) Through QUdpSocket
    Please Help Me To Solve This Problem

    My Server Side Code :

                                            MainWindow::MainWindow(QWidget *parent) :                                            
                                                QMainWindow(parent),                                            
                                                ui(new Ui::MainWindow)                                            
                                            {                                            
                                                ui->setupUi(this);                                            
                                                socket = new QUdpSocket(this);                                                                                                                                                                                
                                            
                                                //////////////////////// Get Image From Client /////////////////////////////////                                                                                        
                                            
                                                connect(socket ,&QUdpSocket::readyRead, [&]()                                            
                                                {                                            
                                                    if(socket->hasPendingDatagrams())                                            
                                                    {                                            
                                                        QByteArray Datagram = socket->readAll();                                                                                                                                    
                                                        QFile target;                                            
                                                        target.setFileName("D:/1.jpg");                                                                                                                                                                                                                                                                                                                             
                                                        if (!target.open(QIODevice::WriteOnly | QIODevice::Append))                                           
                                                        {                                            
                                                            qDebug() << "Can't open file for written";                                            
                                                            return;                                            
                                                        }                                                                                                                                    
                                                        target.write(Datagram);                                            
                                                        target.close();                                            
                                                        QHostAddress sender;                                            
                                                        quint16 senderPort;                                            
                                                        Datagram.resize(socket->pendingDatagramSize());                                            
                                                        socket->readDatagram(Datagram.data(), Datagram.size(), &sender, &senderPort);                                            
                                                        ui->textEdit->setText(QString(Datagram));                                            
                                                    }                                            
                                                });                                            
                                            }                                                                                        
                                            
                                            MainWindow::~MainWindow()                                            
                                            {                                            
                                                delete ui;                                            
                                            }                                            
                                                                                        
                                            void MainWindow::on_pushButton_clicked()                                            
                                            {                                                                                                                                                                                                                            
                                                //////////////////////// Send Image To Client /////////////////////////////////                                                                                        
                                            
                                                QFile file("D:/Image.jpg");                                                                                        
                                            
                                                if(!file.open(QIODevice::ReadWrite))                                            
                                                   qDebug() << "Can't Send";
                                                                                                                                  
                                                auto datagram = file.readAll();
    
                                                socket->bind(ui->spinBox->value(), QUdpSocket::ShareAddress);
                                            
                                                socket->writeDatagram(datagram, QHostAddress(ui->txt_Ip->text()), ui->spinBox->value());                                            
                                            
                                            }
    

    My Client Side Code :

                                     MainWindow::MainWindow(QWidget *parent) :
                                         QMainWindow(parent),
                                         ui(new Ui::MainWindow)
                                     {
                                         ui->setupUi(this);
                                         socket = new QUdpSocket(this);
    
                                         ////////////////////////////// Read Image From Server ////////////////////////
    
                                         connect(socket ,&QUdpSocket::readyRead, [&]()
                                         {                                    
                                             if(socket->hasPendingDatagrams())                                     
                                             {                                     
                                                 QByteArray Datagram = socket->readAll();
    
                                                 QFile target;
                                     
                                                 target.setFileName("/home/pi/Desktop/Test.jpg");
                                                                                                           
                                                 if (!target.open(QIODevice::WriteOnly | QIODevice::Append))                                     
                                                 {                                     
                                                     qDebug() << "Can't open file for written";                                     
                                                     return;                                     
                                                 }                                     
                                                 target.write(Datagram);                                     
                                                 target.close();                                     
                                                 QHostAddress sender;                                     
                                                 quint16 senderPort;                                     
                                                 Datagram.resize(socket->pendingDatagramSize());                                     
                                                 socket->readDatagram(Datagram.data(), Datagram.size(), &sender, &senderPort);                                     
                                                 ui->textEdit->setText(QString(Datagram));                                     
                                             }                                     
                                         });                                     
                                     }               
                                                                                             
                                     MainWindow::~MainWindow()                                     
                                     {                                     
                                         delete ui;                                     
                                     }
    
                                     void MainWindow::on_pushButton_clicked()                                     
                                     {                                                                                                                   
                                         //////////////////////////////////////////// Send Image To Server //////////////////////////////
                                                                                                                   
                                         QFile file("/home/pi/Desktop/Images/Vision_2.jpg");
                                                                                                               
                                         if(!file.open(QIODevice::ReadWrite))                                     
                                            qDebug() << "Can't Send";
                                                                                                               
                                         auto datagram = file.readAll();
                                                                                                               
                                         socket->bind(ui->spinBox->value(), QUdpSocket::ShareAddress);                                     
                                         socket->writeDatagram(datagram, QHostAddress("***.***.**.**"), ui->spinBox->value());                                     
                                     }
    

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    Please Help Me For Correct My Code

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    JonBJ 1 Reply Last reply
    0
    • Ketan__Patel__0011K Ketan__Patel__0011

      Hello Experts

      I Want To Share Any File Like (Image File , etc..) Through QUdpSocket
      Please Help Me To Solve This Problem

      My Server Side Code :

                                              MainWindow::MainWindow(QWidget *parent) :                                            
                                                  QMainWindow(parent),                                            
                                                  ui(new Ui::MainWindow)                                            
                                              {                                            
                                                  ui->setupUi(this);                                            
                                                  socket = new QUdpSocket(this);                                                                                                                                                                                
                                              
                                                  //////////////////////// Get Image From Client /////////////////////////////////                                                                                        
                                              
                                                  connect(socket ,&QUdpSocket::readyRead, [&]()                                            
                                                  {                                            
                                                      if(socket->hasPendingDatagrams())                                            
                                                      {                                            
                                                          QByteArray Datagram = socket->readAll();                                                                                                                                    
                                                          QFile target;                                            
                                                          target.setFileName("D:/1.jpg");                                                                                                                                                                                                                                                                                                                             
                                                          if (!target.open(QIODevice::WriteOnly | QIODevice::Append))                                           
                                                          {                                            
                                                              qDebug() << "Can't open file for written";                                            
                                                              return;                                            
                                                          }                                                                                                                                    
                                                          target.write(Datagram);                                            
                                                          target.close();                                            
                                                          QHostAddress sender;                                            
                                                          quint16 senderPort;                                            
                                                          Datagram.resize(socket->pendingDatagramSize());                                            
                                                          socket->readDatagram(Datagram.data(), Datagram.size(), &sender, &senderPort);                                            
                                                          ui->textEdit->setText(QString(Datagram));                                            
                                                      }                                            
                                                  });                                            
                                              }                                                                                        
                                              
                                              MainWindow::~MainWindow()                                            
                                              {                                            
                                                  delete ui;                                            
                                              }                                            
                                                                                          
                                              void MainWindow::on_pushButton_clicked()                                            
                                              {                                                                                                                                                                                                                            
                                                  //////////////////////// Send Image To Client /////////////////////////////////                                                                                        
                                              
                                                  QFile file("D:/Image.jpg");                                                                                        
                                              
                                                  if(!file.open(QIODevice::ReadWrite))                                            
                                                     qDebug() << "Can't Send";
                                                                                                                                    
                                                  auto datagram = file.readAll();
      
                                                  socket->bind(ui->spinBox->value(), QUdpSocket::ShareAddress);
                                              
                                                  socket->writeDatagram(datagram, QHostAddress(ui->txt_Ip->text()), ui->spinBox->value());                                            
                                              
                                              }
      

      My Client Side Code :

                                       MainWindow::MainWindow(QWidget *parent) :
                                           QMainWindow(parent),
                                           ui(new Ui::MainWindow)
                                       {
                                           ui->setupUi(this);
                                           socket = new QUdpSocket(this);
      
                                           ////////////////////////////// Read Image From Server ////////////////////////
      
                                           connect(socket ,&QUdpSocket::readyRead, [&]()
                                           {                                    
                                               if(socket->hasPendingDatagrams())                                     
                                               {                                     
                                                   QByteArray Datagram = socket->readAll();
      
                                                   QFile target;
                                       
                                                   target.setFileName("/home/pi/Desktop/Test.jpg");
                                                                                                             
                                                   if (!target.open(QIODevice::WriteOnly | QIODevice::Append))                                     
                                                   {                                     
                                                       qDebug() << "Can't open file for written";                                     
                                                       return;                                     
                                                   }                                     
                                                   target.write(Datagram);                                     
                                                   target.close();                                     
                                                   QHostAddress sender;                                     
                                                   quint16 senderPort;                                     
                                                   Datagram.resize(socket->pendingDatagramSize());                                     
                                                   socket->readDatagram(Datagram.data(), Datagram.size(), &sender, &senderPort);                                     
                                                   ui->textEdit->setText(QString(Datagram));                                     
                                               }                                     
                                           });                                     
                                       }               
                                                                                               
                                       MainWindow::~MainWindow()                                     
                                       {                                     
                                           delete ui;                                     
                                       }
      
                                       void MainWindow::on_pushButton_clicked()                                     
                                       {                                                                                                                   
                                           //////////////////////////////////////////// Send Image To Server //////////////////////////////
                                                                                                                     
                                           QFile file("/home/pi/Desktop/Images/Vision_2.jpg");
                                                                                                                 
                                           if(!file.open(QIODevice::ReadWrite))                                     
                                              qDebug() << "Can't Send";
                                                                                                                 
                                           auto datagram = file.readAll();
                                                                                                                 
                                           socket->bind(ui->spinBox->value(), QUdpSocket::ShareAddress);                                     
                                           socket->writeDatagram(datagram, QHostAddress("***.***.**.**"), ui->spinBox->value());                                     
                                       }
      

      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

      Please Help Me For Correct My Code

      /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Ketan__Patel__0011 said in How to Share Any File Using QUdpSocket:

      Please Help Me To Solve This Problem
      Please Help Me For Correct My Code

      In all this code you don't want to tell us what needs solving/correcting, you want us to do all the work to find out? When posting a question it is usual to supply this information....

      On a general matter, however. If I understand right, you are trying to use UDP to send the content of files around?? Since UDP is a lossy protocol (i.e. no guarantee all packets get delivered anywhere), and also no guarantee on reception order, it's not going to be suitable for transferring file content!. You will need TCP for that....

      Ketan__Patel__0011K 1 Reply Last reply
      3
      • JonBJ JonB

        @Ketan__Patel__0011 said in How to Share Any File Using QUdpSocket:

        Please Help Me To Solve This Problem
        Please Help Me For Correct My Code

        In all this code you don't want to tell us what needs solving/correcting, you want us to do all the work to find out? When posting a question it is usual to supply this information....

        On a general matter, however. If I understand right, you are trying to use UDP to send the content of files around?? Since UDP is a lossy protocol (i.e. no guarantee all packets get delivered anywhere), and also no guarantee on reception order, it's not going to be suitable for transferring file content!. You will need TCP for that....

        Ketan__Patel__0011K Offline
        Ketan__Patel__0011K Offline
        Ketan__Patel__0011
        wrote on last edited by
        #3

        @JonB said in How to Share Any File Using QUdpSocket:

        going

        Thanks For The Reply

        I Have Multiple Client And One Server,
        And I Want To Listen Any One Client At Time So Whatever File Send By Client That Time Server Get The Response Of Client

        I Know About TcpSocket And TcpServer And I was Also Try With IT

        But I Can't Listen Any One Particular Client

        So I Am Moved On QUdpSocket
        In This Concept I Am Able To Listen Any One Client In Same Port Connection

        JonBJ 1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Ketan__Patel__0011 said in How to Share Any File Using QUdpSocket:

          But I Can't Listen Any One Particular Client

          This is also not needed - the server opens a port and listens on them... see the examples

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          3
          • Ketan__Patel__0011K Ketan__Patel__0011

            @JonB said in How to Share Any File Using QUdpSocket:

            going

            Thanks For The Reply

            I Have Multiple Client And One Server,
            And I Want To Listen Any One Client At Time So Whatever File Send By Client That Time Server Get The Response Of Client

            I Know About TcpSocket And TcpServer And I was Also Try With IT

            But I Can't Listen Any One Particular Client

            So I Am Moved On QUdpSocket
            In This Concept I Am Able To Listen Any One Client In Same Port Connection

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Ketan__Patel__0011
            In addition to what @Christian-Ehrlicher has written, the fact remains that UDP is not going to be suitable for file transfer, regardless....

            1 Reply Last reply
            3

            • Login

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