You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Monday, May 30th 2011, 4:00pm

file transfer

can anyone please tell me how to transfer files other than text using QTcpSocket and QDataStream.
i want to transfer a video.I can transfer text files but when i try for other formats output files get corrupted.
if you have some sample code please share.

2

Monday, May 30th 2011, 4:10pm

this is the sender side of code



void server::sendfile()
{
//create a socket in serevr side
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();

connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
//code for reading the file
QFile inputFile(":/a.docx");
inputFile.open(QIODevice::ReadOnly);


QDataStream in(&inputFile);
QByteArray line ;
in >> line ;
inputFile.close();

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << line;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));

clientConnection->write(block);
clientConnection->disconnectFromHost();
}



and receiver side of code



QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_0);
if (blockSize == 0) {
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return ;
in >> blockSize;
}
if (tcpSocket->bytesAvailable() < blockSize)
return;
QByteArray line;
in >> line;
QFile file("E:/bin_trans/client/b.docx");
if(!(file.open(QIODevice::Append)))
QMessageBox::information(this, tr("File"),tr("File cannot be opened."));
QDataStream dataStrmForFile(&file);

dataStrmForFile<<line;

line="";
blockSize=0;


file.close();

3

Sunday, June 5th 2011, 11:46pm

Hey ddnith,

I'm also working on a project in which I want to send all kind of files over tcp. I succesfully sended pictures already. In my tests with mp3 files, they also got corrupted. My code is similar to yours, but with my debugging it seems that if the files becomes to big, the receiving slot gets fired several times due to package transmission. They must be appended very carefully. I believe that a more robust file reconstruction protocol must be implemented.
I send my code example tomorrow, I think we can fix this together.

Kind regards,
Esquive

4

Monday, June 6th 2011, 3:23pm

Hi Esquive
I have solved the problem almost. Now I am able to send Large files over network.
problem in my previous code was that I was closing the file before it was completely written.
If you like you can have a look at these samples.
with these codes i am able to send data of upto 300 mb on windows platform.
if you want to send larger files try sending them in chunks.


//sender code

{
//create a socket in serevr side
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();

connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
//code for reading the file
QFile inputFile("path to file to be read");
inputFile.open(QIODevice::ReadOnly);

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint32)0;
QByteArray qwerty=inputFile.readAll();


block.append(qwerty);

inputFile.close();
out.device()->seek(0);
out << (quint32)(block.size() - sizeof(quint32));

qint64 x=0;
while(x<block.size())
{
qint64 y=clientConnection->write(block);
x=x+y;
qDebug()<<x;
}

clientConnection->disconnectFromHost();
}




//receiver code

{
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_0);
if (blockSize == 0)
{
if (tcpSocket->bytesAvailable() < sizeof(quint32))
return ;


in >> blockSize;
}
if (tcpSocket->bytesAvailable() < blockSize)
return ;

QByteArray line;
line= tcpSocket -> readAll();
QFile file("D:/drdo/qt_apps/done/june1/client/x.avi");
if(!(file.open(QIODevice::Append)))
QMessageBox::information(this, tr("File"),tr("File cannot be opened."));
file.write(line);


blockSize=0;


file.close();
}

:)

5

Monday, June 6th 2011, 4:50pm

This is almost how I did it. For now I' m not buffering the file within a QDataStream. To keep it simple I use:
I'm not closing the file before it is written. Do you think the behavior I'm encountering is due to the fact that I'm not buffering?

Sender:

void client::sendFile(QString inFilePath)
{
QFile* myFile = new QFile(inFilePath);

myFile->open(QIODevice::ReadOnly);

this->mySocket->write(myFile->readAll());
}



Receiver:


void server::read()
{
QByteArray data = this->mySocket->readAll();

QFile* entry = new QFile("test.mp3");

if(entry->open(QIODevice::WriteOnly))
{
entry->write(data);
QDataStream toFile(entry);
toFile << data;
entry->close();
}

delete entry;
}

6

Monday, June 6th 2011, 5:10pm

So i just fixed it for the mp3 file. I'm now trying larger files.

The problem was on the receiving side:


Receiver:


void server::read()
{
QByteArray data = this->mySocket->readAll();

QFile* entry = new QFile("test.mp3");

if(entry->open(QIODevice::WriteOnly)) //HERE I HAVE TO OPEN THE FILE IN THE APPEND MODE.
{
entry->write(data);
QDataStream toFile(entry);
toFile << data; //OBVIOUSLY I WAS WRITING THE DATA TO THE FILE TWICE. ^^
entry->close();
}

delete entry;
}

Now the code looks like:

void server::read()
{
QByteArray data = this->mySocket->readAll();

QFile* entry = new QFile("test.mp3");

if(entry->open(QIODevice::Append))
{
entry->write(data);
entry->close();
}

delete entry;
}

Quiet Simple, now it still has to work for very large files.

7

Tuesday, June 14th 2011, 6:18am

Study torrent client application code

Torrent client is a file sharing concept between two or more machines which is connected through LAN. If you read torrent application source code, you can get more knowledge about file transfer or recieving concepts. You can found torrent client source code at '\examples\network\torrent'. This folder is available with Qt.

Similar threads