PDA

View Full Version : Transfert of File with QTcpSocket



Stanfillirenfro
28th August 2013, 16:39
Dear freinds,

I am facing a problem of transferring a file in a mini chat.
After clicking on the upload buton, I get directory where the file is saved, but I can find file itself.

Here is my code:



void FenClient::sendFile()
{
QString fileName = QFileDialog::getOpenFileName(this, "Information", "Open a file",
"File(*.text *.png *.jpeg *.gif)");
m_file = fileName;
QFile file(fileName);

if(!file.open(QIODevice::ReadOnly))
{
return;
}

QByteArray paquet = file.readAll();
QDataStream out(&paquet, QIODevice::WriteOnly);
out<< (quint32) 0;
out<< (quint32)(file.size()-sizeof(quint32));
m_socket->write(paquet);
}


Could you please tell me what is missing in my code and how to fix the problem?

Many thanks.

wysota
28th August 2013, 17:59
Eeeem.... what exactly did you expect to obtain with the above code?

Stanfillirenfro
28th August 2013, 22:20
I just want to download a file and to transfert it in a mini chat (Server/Client)

wysota
29th August 2013, 06:55
I just want to download a file and to transfert it in a mini chat (Server/Client)

This code does not "download" anything neither does it "transfer" anything to a "mini chat". What it does is that it reads a file and overwrites its first couple of bytes with two serialized integers one of which is 0 and the other is size of the file decreased by four. Then it writes that broken file to m_socket.

Thus I'm asking you again to state what you expected this code to do. We already know what it does and we can assume you wanted it to do something else so please state what was the expected outcome of running that function.

Stanfillirenfro
29th August 2013, 11:23
Many thanks Wysota for your reply.

This is the situation. I have written a code of a mini chat. In this code, I wanted to be able to transfert data to clients. What I have sent was just a piece of my code. This piece of code should open a directory, read a file, copy it and sent it to different cilents.
I did not send the piece of code of the server, just part for the client.

I would appreciate your help.

Reception slot



void WinClient::MessageRecieved()
{
QString messageRecived;
QDataStream in(m_socket);
if(sizeMessage == 0)
{
if(m_socket->bytesAvailable() < (int)sizeof(quint16))
{
return;
}
in>>sizeMessage;
}

if(m_socket->bytesAvailable() < sizeMessage)
{
return;
}

in >> messageRecieved;

m_zoneMessage->append(messageRecu);
sizeMessage = 0;
}



Sending slot



void WinClient::clicOnBoutonConnection()
{
m_zoneMessage->append(tr("<em>Attempt of connexion...</em>"));
m_boutonConnexion->setEnabled(false);
m_socket->abort();
m_socket->connectToHost(m_textIp->text(), m_ServerPort->value());
}


Many thanks in advance

wysota
29th August 2013, 11:35
This piece of code should open a directory, read a file, copy it and sent it to different cilents.
As already stated it does not do that. It wrecks the file by overwriting the first bytes and then dumps that into the socket.

I'm not sure how you wish to differentiate between a "chat message" and a "file" on the receiving end if you are not using any kind of protocol for communication but instead you rely on the broken fortune cookie example without even understanding how it works.

Stanfillirenfro
30th August 2013, 07:03
Thanks Wysota for your explaination.

Since I am new on this point, could you please give me a path how to fix the problem? To my knowledge, the server should receive and send the "chat message" in the same way like a file, the only difference being th esize of each. PLease corect me if I am not correct.

Thanks in advance

wysota
30th August 2013, 07:55
Since I am new on this point, could you please give me a path how to fix the problem?
The path is to design a communication protocol first before you start network programming.


To my knowledge, the server should receive and send the "chat message" in the same way like a file, the only difference being th esize of each. PLease corect me if I am not correct.
How should I know? It's your server not mine. If you claim the receiving end should be the same then I'll say that the sending end should be the same as well. However I have no idea how you wish to display an image file as text in the chat window.