Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to share full folder using TCP Server Socket



  • 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


  • Lifetime Qt Champion

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


  • Lifetime Qt Champion

    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
    }
    

  • Lifetime Qt Champion

    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 ?



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

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


  • Lifetime Qt Champion

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



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

    to

    i want to send All files with its Contents


  • Lifetime Qt Champion

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



  • @jsulm I dosen't Have That Code

    If You Have That Code

    So plz Send Me


  • Lifetime Qt Champion

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



  • @jsulm Okay Thanks For Help


  • Lifetime Qt Champion

    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.



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

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


  • Lifetime Qt Champion

    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.


Log in to reply