PDA

View Full Version : Receive file over TCP



winarko
28th May 2008, 13:52
Can anyone give me a sample program how to receive a binary files over TCP using QSocket or other similar class?

Thx in advance..

lyuts
28th May 2008, 14:18
Can anyone give me a sample program how to receive a binary files over TCP using QSocket or other similar class?

Thx in advance..

http://doc.trolltech.com/4.4/examples.html#network

winarko
28th May 2008, 17:32
Thanks but I still have a problem. I'll let u know my project is:
1.Build a Simple HTTP Server on embedded device so that a browser can connect and receive a HTML page.(done)
2.Let the browser (client PC) upload a GIF file to that server. I use FrontPage2003 to develop a HTML script and FileUpload Component from FrontPage2003. (not done yet)
The problem is my embedded device can't receive the whole packets transfered, it only can receive some part of the file.
Do u have any solution??

Thx in advance.

MaximA
28th May 2008, 17:52
The problem is my embedded device can't receive the whole packets transfered, it only can receive some part of the file.
Why? buffer to small in the embedded device? Where happens the fragmentation?

winarko
28th May 2008, 17:59
This is my read socket procedure:


while (socket->bytesAvailable())
{
myfile.writeBlock(socket->readAll());
myfile.flush();
socket->waitForMore(1);
}

When I simulated this program on PC (using localhost address 127.0.0.1), I can get the whole part of the file I sent. But unfortunately I can't do that for my embedded device.

Any suggestion??

Thx for reply

MaximA
28th May 2008, 18:11
maybe I'm missing something...You have to interpret the length-key of the httpheader somewhere.

winarko
28th May 2008, 18:19
Maybe I should try to find another way...

MaximA
28th May 2008, 18:34
Maybe I should try to find another way...
Do you understand the problem?

Example:
the file size is 100.000 Bytes
the 1st tcp/ip paket has 50.000 Bytes, when it arrives it triggers your slot, you read it, and think that was all
in the meantime the 2nd paket arrives, but you don't expect it, you think that all the data has been send in the first paket which triggert the slot.

Understandable?

winarko
29th May 2008, 12:10
in the meantime the 2nd paket arrives, but you don't expect it, you think that all the data has been send in the first paket which triggert the slot.



Look at my program:

while (socket->bytesAvailable()) // In this loop, I wait for new incoming packet until no packet arrived anymore
{
myfile.writeBlock(socket->readAll());
myfile.flush();
socket->waitForMore(1);
}

spud
29th May 2008, 12:18
from the Qt Docs:


qint64 QAbstractSocket::bytesAvailable () const [virtual]
Returns the number of incoming bytes that are waiting to be read.

That does not mean that there aren't more coming.

winarko
29th May 2008, 14:23
That does not mean that there aren't more coming.

Do you know while loop??? If the value in the bracket "WHILE(value)" is zero, the loop ends, and when the value greater than zero (that means there are still a data in socket), the loop still work.:mad:

OK,can u give me a sample code to receive continuous data from socket?

lyuts
29th May 2008, 14:35
What about forever loop like

for ( ; ; ) {
...
}

or a better representation of it

forever {
...
}

? Is it ok for you?

winarko
29th May 2008, 14:43
What about forever loop like

for ( ; ; ) {
...
}

or a better representation of it

forever {
...
}

? Is it ok for you?

huh?:eek:

hahahahahaha....:D

I think you have to learn more about Qt Programming.
Do u know, if u write that stupid loop in Real Time O.S programming, your program will be hang up on that loop:p

spud
29th May 2008, 14:46
Do you know while loop???
Yes, but I don't think that's what we're discussing here.

OK,can u give me a sample code to receive continuous data from socket?
You need to send the information how many bytes are to be expected. There is no other reasonable break condition for the loop.

winarko
29th May 2008, 14:57
You need to send the information how many bytes are to be expected. There is no other reasonable break condition for the loop.

I can't determine the exactly bytes I expexted, because it is depended on the file I sent. Just give me some example to receive file from another PC, and I will give my highest appreciation to you.;)

spud
29th May 2008, 15:19
I can't determine the exactly bytes I expextedWhich is why you need to send it.
I suggest you read the documentation for the fortune client/server example.


void Server::sendFortune()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << fortunes.at(qrand() % fortunes.size());
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
}

void Client::readFortune()
{
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;

QString nextFortune;
in >> nextFortune;
}

lyuts
29th May 2008, 16:07
huh?:eek:

hahahahahaha....:D

I think you have to learn more about Qt Programming.
Do u know, if u write that stupid loop in Real Time O.S programming, your program will be hang up on that loop:p

=) The same occurs to you... If you are writing a program using a socket then (first of all) look through the documentation on that...

you'll find that there is such a signal readyRead() which you can connect to a slot... in this way you can do what you need.

jacek
29th May 2008, 17:02
huh?:eek:

hahahahahaha....:D

I think you have to learn more about Qt Programming.
Do u know, if u write that stupid loop in Real Time O.S programming, your program will be hang up on that loop:p
I see you like to encourage people to anwer your questions. Remember that everyone here answers in his/her own free time and has no obligation to do so.

MaximA
29th May 2008, 17:25
Going back to the op problem:


2.Let the browser (client PC) upload a GIF file to that server. ...

My answer: ...You have to interpret the length-key of the httpheader somewhere.


I can't determine the exactly bytes I expexted, because it is depended on the file I sent...
Of cause you can't determine it, if you haven't looked for the header.

If you send it via browser POST, then I think you should be able to determine it.

For better understanding: Dump all the data transferred to a text file


qDebug << socket->readAll();
// or
qDebug << socket->readAll().toHex();
and have a look! if there isn't a http-header let us know (and if you post the dump: send the first few bytes only! we don't need the gif-data!)

It seems, you haven't understood http (which isn't easy)

These would be suggested resources:
http://www.faqs.org/rfcs/rfc2616.html
http://doc.trolltech.com/4.4/qhttpheader.html

Happy insight!