PDA

View Full Version : binary file sending using qt4.3



omprakash
19th May 2008, 10:33
dear friends,
i am using qt 4.3 for my tcp socket programming.
in my program i am able to send normal text file from one client application to server application.
But i am not able to send the binary file or exe file from client application to server application..Plz if any body is having the sample code plz share with me...

aamer4yu
19th May 2008, 10:47
Are you opening the file as TEXT file ??
can we see the code ?

omprakash
21st May 2008, 18:23
Dear friend this is my sending side code for text file transfer..


void client::sendFile()
{
QFile file("new.txt");
if(!(file.open(QIODevice::ReadOnly)))
QMessageBox::information(this, tr("File"),tr("File cannot be opened."));



QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;

while (!file.atEnd())
{
QByteArray line = file.readLine();
QString om(line);

out<<om;

out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpclient->write(block);

}

file.close();
}


Then this is my receive side code for text file



void server::readClient()
{
QDataStream in(clientConnection);
in.setVersion(QDataStream::Qt_4_0);

QFile file("new_file.txt");
if(!(file.open(QIODevice::Append)))
QMessageBox::information(this, tr("File"),tr("File cannot be opened."));
QTextStream txtStrmForFile(&file);


forever
{
if (blockSize == 0)
{
if (clientConnection->bytesAvailable() < (int)sizeof(quint16))
return;

in >> blockSize;
}



if (clientConnection->bytesAvailable() < blockSize)
return;

QString strData;
in>>strData;


txtStrmForFile<<strData;

strData="";
blockSize=0;
}

file.close();
}


This works fine as far as new.txt is a text file..but for other files e.g. for any other file this is not working..plz anybody can modify this code and suggest what to be done ..This code is in Qt4.3.3.
bye

dreamevil
22nd May 2008, 20:56
I suppose the problem is here:

QByteArray line = file.readLine();
QString om(line);
Using readLine doesn't make sense with binary files. And there is no need to convert binary data to unicode.