Unsolved How to share full folder using TCP Server Socket
-
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 TcpServerI 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 LocationYou Can Se My Code My Error Is When
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
@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
-
@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__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
-
Hi
You can read the file likeQFile 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. -
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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
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.