PDA

View Full Version : QTcp[server and socket]: can't read my transfered file, why!!



cerina
29th September 2012, 15:52
Good morning,
I tried to create my own code. I have an application, in which, the user will choose a file from his DD ( all types) and send it to the TcpServer, this server will then send this file to the other clients.But, I have a problem, when i choose the file and i send it, in the client’s side, i have this message: file is sending , but in the server’s side, it shows me that the file isn’t recieved with it’s totaly bytes. Any suggestion please. This is the function for sending the file in the client’s side:

void FenClient::on_boutonEnvoyer_2_clicked()
{
QString nomFichier = lineEdit->text();
QFile file(lineEdit->text());
if(!file.open(QIODevice::ReadOnly))
{
qDebug() << "Error, file can't be opened successfully !";
return;

}

QByteArray bytes = file.readAll();

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);

out << quint32(0);
out << nomFichier;
out << bytes;
out.device()->seek(0);
out << quint32((block.size() - sizeof(quint32)));

qDebug() << "Etat : envoi en cours...";
listeMessages->append("status : sending the file...");

socket->write(block);
}
and the server side:

void FenServeur::datarecieved()
{

QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());

if(socket == 0)
{
qDebug() << "no Socket!";
return;
}

forever
{
QDataStream in(socket);
if(blockSize == 0)
{
if(socket->bytesAvailable() )
{
qDebug() << "Error < sizeof(quint32))";
return;
}

in >> blockSize;
}

if(socket->bytesAvailable() < blockSize)
{
qDebug() << "data not recieved with its total bytes";

return;
}

qDebug() << "!!!!!!";
QByteArray dataOut;
QString nameFile;
in >> nameFile >> dataOut;
QFile fileOut(nameFile);
fileOut.open(QIODevice::WriteOnly);
fileOut.write(dataOut);
fileOut.close();

blockSize = 0;
}
}

void FenServeur::sendToAll(const QString &message)
{

QByteArray paquet;
QDataStream out(&paquet, QIODevice::WriteOnly);

out << (quint32) 0;
out << message;
out.device()->seek(0);
out << (quint32) (paquet.size() - sizeof(quint32));
for (int i = 0; i < clients.size(); i++)
{
clients[i]->write(paquet);
}

}

So i can't write the file that the server recieved into a new file, or i have to write it in a filejust in the client side!!!. Any suggestion please!! and thanks in advance

wysota
29th September 2012, 17:55
Do you think posting the same problem in two threads is going to give you twice as many solutions to your problem?

http://www.qtcentre.org/threads/51354-QTcp-server-and-socket-can-t-read-file-sent

cerina
29th September 2012, 20:22
certainly no :eek: , i tried to delete the other post, but i couldn't do it , sorry