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.
  • K Offline
    K Offline
    Ketan__Patel__0011
    wrote on 15 Apr 2020, 06:45 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

    J 1 Reply Last reply 15 Apr 2020, 06:48
    0
    • K Ketan__Patel__0011
      15 Apr 2020, 06:45

      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

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 15 Apr 2020, 06:48 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

      K 1 Reply Last reply 15 Apr 2020, 09:38
      1
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 15 Apr 2020, 06:52 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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 15 Apr 2020, 06:55 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
          • J jsulm
            15 Apr 2020, 06:48

            @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"?

            K Offline
            K Offline
            Ketan__Patel__0011
            wrote on 15 Apr 2020, 09:38 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

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

            J 1 Reply Last reply 15 Apr 2020, 09:46
            0
            • K Ketan__Patel__0011
              15 Apr 2020, 09:38

              @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

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

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 15 Apr 2020, 09:46 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

              K 1 Reply Last reply 15 Apr 2020, 10:52
              0
              • J jsulm
                15 Apr 2020, 09:46

                @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.

                K Offline
                K Offline
                Ketan__Patel__0011
                wrote on 15 Apr 2020, 10:52 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

                J 1 Reply Last reply 15 Apr 2020, 10:56
                0
                • K Ketan__Patel__0011
                  15 Apr 2020, 10:52

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

                  to

                  i want to send All files with its Contents

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 15 Apr 2020, 10:56 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

                  K 1 Reply Last reply 15 Apr 2020, 11:26
                  1
                  • J jsulm
                    15 Apr 2020, 10:56

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

                    K Offline
                    K Offline
                    Ketan__Patel__0011
                    wrote on 15 Apr 2020, 11:26 last edited by
                    #9

                    @jsulm I dosen't Have That Code

                    If You Have That Code

                    So plz Send Me

                    J 1 Reply Last reply 15 Apr 2020, 11:27
                    0
                    • K Ketan__Patel__0011
                      15 Apr 2020, 11:26

                      @jsulm I dosen't Have That Code

                      If You Have That Code

                      So plz Send Me

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 15 Apr 2020, 11:27 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

                      K 1 Reply Last reply 15 Apr 2020, 12:14
                      0
                      • J jsulm
                        15 Apr 2020, 11:27

                        @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...

                        K Offline
                        K Offline
                        Ketan__Patel__0011
                        wrote on 15 Apr 2020, 12:14 last edited by
                        #11

                        @jsulm Okay Thanks For Help

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 15 Apr 2020, 12:21 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.

                          K 1 Reply Last reply 17 Apr 2020, 04:32
                          3
                          • M mrjj
                            15 Apr 2020, 12:21

                            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.

                            K Offline
                            K Offline
                            Ketan__Patel__0011
                            wrote on 17 Apr 2020, 04:32 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
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 17 Apr 2020, 06:01 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

                              1/14

                              15 Apr 2020, 06:45

                              • Login

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