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 full folder using TCP Server Socket
Forum Updated to NodeBB v4.3 + New Features

How to share full folder using TCP Server Socket

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 1.3k Views 2 Watching
  • 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 Friends Now I am working on networking Application

    i am able to share single image or single file using TCP Server socket application

    but Some problem are raised to share full folder using TCP Server Socket application

    Plz Help Me to solve this problem

    thanks in Advance

    jsulmJ 1 Reply Last reply
    0
    • Ketan__Patel__0011K Ketan__Patel__0011

      Hello Friends Now I am working on networking Application

      i am able to share single image or single file using TCP Server socket application

      but Some problem are raised to share full folder using TCP Server Socket application

      Plz Help Me to solve this problem

      thanks in Advance

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Ketan__Patel__0011 said in How to share full folder using TCP Server Socket:

      but Some problem are raised to share full folder using TCP

      What exact problem? Nobody can help to solve a problem without knowing what the problem is.
      What exactly do you mean by "share"?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Ketan__Patel__0011K 1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        You cannot "share" a folder but you can send each file in it to the server.

        //assume the directory exists and contains some files 
        QDir directory("YOURPATH");
        QStringList files= directory.entryList(QStringList() << "*.*",QDir::Files);
        foreach(QString filename, files) {
        //send each file
        }
        
        1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          From your description, it seems that you are trying to re-invent some complex wheel like CIFS, NFS, SFTP or maybe solutions like ownCloud/nextCloud.

          What exactly is your goal ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • jsulmJ jsulm

            @Ketan__Patel__0011 said in How to share full folder using TCP Server Socket:

            but Some problem are raised to share full folder using TCP

            What exact problem? Nobody can help to solve a problem without knowing what the problem is.
            What exactly do you mean by "share"?

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

            @jsulm

            My Server Application Code Is :

            if(socket)
            {
                if(socket->isOpen())
                {
                    QString str = this->ui->lineEdit_message->text();
            
                    QDir directory("/home/pi/Desktop/");
                    QStringList files = directory.entryList(QStringList() << "*.*",QDir::Files);
            
                    qDebug() << files;
            
                    foreach(QString filename, files)
                    {
                        mBuff.append(filename);
                    }
            
                    qDebug() << mBuff; // QByteArray
            
                    socket->flush();
                    socket->write(mBuff);
            
                    if(!socket->waitForBytesWritten(-1))
                    {
                        qDebug() << "writen Bytes error " << socket->errorString();
                    }
                }
                else
                {
                    QMessageBox::critical(this,"QTCPServer","Socket doesn't seem to be opened");
                }
            }
            else
            {
                QMessageBox::critical(this,"QTCPServer","Not connected");
            }
            

            My Client Application Code :

                               QString fPath("D:/Test/");                                           
                               QByteArray block = socket->readAll();                                                         
                               qDebug() << block;                                                         
                               QString fName= QString(block);                   
                               QFile sentFile(fPath + QDir::separator() + fName);                                                         
                               qDebug() << sentFile;                                                         
                               qDebug() << sentFile.exists();                                                         
                              
                              if(sentFile.exists())                   
                               {                   
                                   sentFile.open(QIODevice::WriteOnly);                  
                                   QByteArray block= socket->readAll();                   
                                   qDebug()<< "read:  " << block.size();                   
                                   sentFile.write(block);                   
                                   block.clear();                   
                                   socket->close();                   
                                   sentFile.close();                   
                               }
            

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

            My Target Is I Want To Send Multiple Image Or Files Of Any Particular Folder
            using TcpSocket And TcpServer

            I Am good to Send File From The Server Application But in Client Side
            When I Try Save That All file From The Socket I Can't Write In The My
            "Test" Folder Location

            You Can Se My Code My Error Is When

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

            jsulmJ 1 Reply Last reply
            0
            • Ketan__Patel__0011K Ketan__Patel__0011

              @jsulm

              My Server Application Code Is :

              if(socket)
              {
                  if(socket->isOpen())
                  {
                      QString str = this->ui->lineEdit_message->text();
              
                      QDir directory("/home/pi/Desktop/");
                      QStringList files = directory.entryList(QStringList() << "*.*",QDir::Files);
              
                      qDebug() << files;
              
                      foreach(QString filename, files)
                      {
                          mBuff.append(filename);
                      }
              
                      qDebug() << mBuff; // QByteArray
              
                      socket->flush();
                      socket->write(mBuff);
              
                      if(!socket->waitForBytesWritten(-1))
                      {
                          qDebug() << "writen Bytes error " << socket->errorString();
                      }
                  }
                  else
                  {
                      QMessageBox::critical(this,"QTCPServer","Socket doesn't seem to be opened");
                  }
              }
              else
              {
                  QMessageBox::critical(this,"QTCPServer","Not connected");
              }
              

              My Client Application Code :

                                 QString fPath("D:/Test/");                                           
                                 QByteArray block = socket->readAll();                                                         
                                 qDebug() << block;                                                         
                                 QString fName= QString(block);                   
                                 QFile sentFile(fPath + QDir::separator() + fName);                                                         
                                 qDebug() << sentFile;                                                         
                                 qDebug() << sentFile.exists();                                                         
                                
                                if(sentFile.exists())                   
                                 {                   
                                     sentFile.open(QIODevice::WriteOnly);                  
                                     QByteArray block= socket->readAll();                   
                                     qDebug()<< "read:  " << block.size();                   
                                     sentFile.write(block);                   
                                     block.clear();                   
                                     socket->close();                   
                                     sentFile.close();                   
                                 }
              

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

              My Target Is I Want To Send Multiple Image Or Files Of Any Particular Folder
              using TcpSocket And TcpServer

              I Am good to Send File From The Server Application But in Client Side
              When I Try Save That All file From The Socket I Can't Write In The My
              "Test" Folder Location

              You Can Se My Code My Error Is When

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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Ketan__Patel__0011 said in How to share full folder using TCP Server Socket:

              mBuff.append(filename);

              How are you going to separate file names on the client side?
              You need a separator, so you know where a file name ends and next one starts.

              On the client side you can't assume that readAll will give you exactly one file name. You need read until you have a complete file name (until you find the file name separator.

              Also, I don't understand: do you only want to send a list of file names or do you want to send content of the files? Because currently your server only sends a list of files.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              Ketan__Patel__0011K 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Ketan__Patel__0011 said in How to share full folder using TCP Server Socket:

                mBuff.append(filename);

                How are you going to separate file names on the client side?
                You need a separator, so you know where a file name ends and next one starts.

                On the client side you can't assume that readAll will give you exactly one file name. You need read until you have a complete file name (until you find the file name separator.

                Also, I don't understand: do you only want to send a list of file names or do you want to send content of the files? Because currently your server only sends a list of files.

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

                @jsulm said in How to share full folder using TCP Server Socket:

                to

                i want to send All files with its Contents

                jsulmJ 1 Reply Last reply
                0
                • Ketan__Patel__0011K Ketan__Patel__0011

                  @jsulm said in How to share full folder using TCP Server Socket:

                  to

                  i want to send All files with its Contents

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Ketan__Patel__0011 Then send it.
                  Currently you're only sending a list of file names. Where is your code to send file content?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Ketan__Patel__0011K 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @Ketan__Patel__0011 Then send it.
                    Currently you're only sending a list of file names. Where is your code to send file content?

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

                    @jsulm I dosen't Have That Code

                    If You Have That Code

                    So plz Send Me

                    jsulmJ 1 Reply Last reply
                    0
                    • Ketan__Patel__0011K Ketan__Patel__0011

                      @jsulm I dosen't Have That Code

                      If You Have That Code

                      So plz Send Me

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Ketan__Patel__0011 No, I don't have that code and I'm not going to write it. You should at least try to write it by yourself...

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      Ketan__Patel__0011K 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @Ketan__Patel__0011 No, I don't have that code and I'm not going to write it. You should at least try to write it by yourself...

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

                        @jsulm Okay Thanks For Help

                        1 Reply Last reply
                        0
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #12

                          Hi
                          You can read the file like

                          QFile file(fileName);
                          if (!file.open(QIODevice::ReadOnly)) return;
                          QByteArray blob = file.readAll();
                          

                          then send the blob

                          Make sure you separate each file content with some marker so you know when you have gotten
                          all that belongs to one file. There will be multiple QByteArray block= socket->readAll();
                          pr image file so make sure you handle that.

                          Ketan__Patel__0011K 1 Reply Last reply
                          3
                          • mrjjM mrjj

                            Hi
                            You can read the file like

                            QFile file(fileName);
                            if (!file.open(QIODevice::ReadOnly)) return;
                            QByteArray blob = file.readAll();
                            

                            then send the blob

                            Make sure you separate each file content with some marker so you know when you have gotten
                            all that belongs to one file. There will be multiple QByteArray block= socket->readAll();
                            pr image file so make sure you handle that.

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

                            @mrjj

                            Hey QT CHAMPION

                            I Am Trying To Send All File Of Any Particular Directory

                            MY Code Is LIke This :

                            My Server Side Application Code Is :

                                                       void MainWindow::sendMessage(QTcpSocket* socket)                               
                                                       {                               
                                                           if(socket)                               
                                                           {                               
                                                               if(socket->isOpen())                               
                                                               {                               
                                                                   QString str = this->ui->lineEdit_message->text();                                                                                             
                                                                   QDir directory("/home/pi/Desktop/Images/");                                                                                             
                                                                  QStringList files = directory.entryList(QStringList() << "*.*", QDir::Files);                                                                                             
                                                                   for(auto i = 0 ; i < files.size() ; i++)                               
                                                                   {                               
                                                                       QFile file(files.at(i));                               
                                                                       qDebug() << file;   
                                                                                                                  
                                                                       if(!file.open(QIODevice::ReadWrite))                               
                                                                           qDebug() << "Can't Send";
                                                                                                                     
                                                                       Data.append(file.read(i));                                                                                                            
                                                       
                                                                       socket->write(Data);
                                                       
                                                                       socket->flush();                                                              
                                                       
                                                                       if(!socket->waitForBytesWritten(3000))                               
                                                                           qDebug() << "writen Bytes error " << socket->errorString();                               
                                                                   }                               
                                                               }                               
                                                               else                               
                                                               {                               
                                                                   QMessageBox::critical(this,"QTCPServer","Socket doesn't seem to be opened");                               
                                                               }                               
                                                           }                               
                                                           else                               
                                                           {                               
                                                               QMessageBox::critical(this,"QTCPServer","Not connected");                               
                                                           }                               
                                                       }
                            

                            My Client Side Application Code Is :

                                                           void MainWindow::readSocket()                                   
                                                           {                                   
                                                               for(int i = 0 ; i < socket->size() ; i++)                                   
                                                               {                                   
                                                                   QByteArray Data = socket->readAll();                                                                                                         
                                                                   qDebug() << Data;                                                                   
                                                                   QFile target;                                   
                                                                   target.setFileName(socket->read(i));                                                                                                                 
                                                                   qDebug() << target.readAll();
                                                                                                                                 
                                                                   if (!target.open(QIODevice::WriteOnly | QIODevice::Append))                                   
                                                                   {                                   
                                                                       qDebug() << "Can't open file for written";                                   
                                                                       return;                                   
                                                                   }                                                                                                         
                                                                   target.write(Data);                                   
                                                                   target.close();                                   
                                                               }                                   
                                                           }
                            

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

                            What's Wrong In This Code

                            I Can't Send multiple File using This Code

                            But This Logic Is Perfectly Work For Single File

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

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Hi,

                              What's wrong is that you don't have any protocol implemented. You just send bytes without any information for your client to know when a file start and when it ends nor what its name is.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              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