Dear friend this is my sending side code for text file transfer..
void client::sendFile()
{
QMessageBox::information(this, tr
("File"),tr
("File cannot be opened."));
out << (quint16)0;
while (!file.atEnd())
{
out<<om;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpclient->write(block);
}
file.close();
}
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();
}
To copy to clipboard, switch view to plain text mode
Then this is my receive side code for text file
void server::readClient()
{
QFile file("new_file.txt");
QMessageBox::information(this, tr
("File"),tr
("File cannot be opened."));
forever
{
if (blockSize == 0)
{
if (clientConnection->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
if (clientConnection->bytesAvailable() < blockSize)
return;
in>>strData;
txtStrmForFile<<strData;
strData="";
blockSize=0;
}
file.close();
}
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();
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks