Results 1 to 19 of 19

Thread: Receive file over TCP

  1. #1
    Join Date
    May 2008
    Posts
    21
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Receive file over TCP

    Can anyone give me a sample program how to receive a binary files over TCP using QSocket or other similar class?

    Thx in advance..

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Receive file over TCP

    Quote Originally Posted by winarko View Post
    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

  3. #3
    Join Date
    May 2008
    Posts
    21
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Receive file over TCP

    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.

  4. #4
    Join Date
    May 2008
    Posts
    15
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Receive file over TCP

    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?

  5. #5
    Join Date
    May 2008
    Posts
    21
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Receive file over TCP

    This is my read socket procedure:

    Qt Code:
    1. while (socket->bytesAvailable())
    2. {
    3. myfile.writeBlock(socket->readAll());
    4. myfile.flush();
    5. socket->waitForMore(1);
    6. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by jpn; 29th May 2008 at 12:11. Reason: missing [code] tags

  6. #6
    Join Date
    May 2008
    Posts
    15
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Receive file over TCP

    maybe I'm missing something...You have to interpret the length-key of the httpheader somewhere.

  7. #7
    Join Date
    May 2008
    Posts
    21
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Receive file over TCP

    Maybe I should try to find another way...

  8. #8
    Join Date
    May 2008
    Posts
    15
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Receive file over TCP

    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?

  9. #9
    Join Date
    May 2008
    Posts
    21
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Receive file over TCP

    Quote Originally Posted by MaximA View Post


    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:
    Qt Code:
    1. while (socket->bytesAvailable()) // In this loop, I wait for new incoming packet until no packet arrived anymore
    2. {
    3. myfile.writeBlock(socket->readAll());
    4. myfile.flush();
    5. socket->waitForMore(1);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 29th May 2008 at 12:11. Reason: missing [code] tags

  10. #10
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Receive file over TCP

    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.

  11. #11
    Join Date
    May 2008
    Posts
    21
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Receive file over TCP

    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.

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

  12. #12
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Receive file over TCP

    What about forever loop like

    for ( ; ; ) {
    ...
    }

    or a better representation of it

    forever {
    ...
    }

    ? Is it ok for you?
    I'm a rebel in the S.D.G.

  13. #13
    Join Date
    May 2008
    Posts
    21
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Receive file over TCP

    Quote Originally Posted by lyuts View Post
    What about forever loop like

    for ( ; ; ) {
    ...
    }

    or a better representation of it

    forever {
    ...
    }

    ? Is it ok for you?
    huh?

    hahahahahaha....

    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

  14. #14
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Receive file over TCP

    Quote Originally Posted by winarko View Post
    Do you know while loop???
    Yes, but I don't think that's what we're discussing here.
    Quote Originally Posted by winarko View Post
    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.

  15. #15
    Join Date
    May 2008
    Posts
    21
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Receive file over TCP

    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.

  16. #16
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Receive file over TCP

    Quote Originally Posted by winarko View Post
    I can't determine the exactly bytes I expexted
    Which is why you need to send it.
    I suggest you read the documentation for the fortune client/server example.
    Qt Code:
    1. void Server::sendFortune()
    2. {
    3. QByteArray block;
    4. QDataStream out(&block, QIODevice::WriteOnly);
    5. out.setVersion(QDataStream::Qt_4_0);
    6. out << (quint16)0;
    7. out << fortunes.at(qrand() % fortunes.size());
    8. out.device()->seek(0);
    9. out << (quint16)(block.size() - sizeof(quint16));
    10. }
    11.  
    12. void Client::readFortune()
    13. {
    14. QDataStream in(tcpSocket);
    15. in.setVersion(QDataStream::Qt_4_0);
    16.  
    17. if (blockSize == 0) {
    18. if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
    19. return;
    20.  
    21. in >> blockSize;
    22. }
    23.  
    24. if (tcpSocket->bytesAvailable() < blockSize)
    25. return;
    26.  
    27. QString nextFortune;
    28. in >> nextFortune;
    29. }
    To copy to clipboard, switch view to plain text mode 

  17. #17
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Receive file over TCP

    Quote Originally Posted by winarko View Post
    huh?

    hahahahahaha....

    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
    =) 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.
    I'm a rebel in the S.D.G.

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Receive file over TCP

    Quote Originally Posted by winarko View Post
    huh?

    hahahahahaha....

    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
    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.

  19. #19
    Join Date
    May 2008
    Posts
    15
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Receive file over TCP

    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

    Qt Code:
    1. qDebug << socket->readAll();
    2. // or
    3. qDebug << socket->readAll().toHex();
    To copy to clipboard, switch view to plain text mode 
    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!

Similar Threads

  1. QtHelp Module - registering compressed help file gives error
    By Ankitha Varsha in forum Qt Programming
    Replies: 1
    Last Post: 16th May 2008, 14:14
  2. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  3. file renaming on windows
    By jdd81 in forum Qt Programming
    Replies: 9
    Last Post: 2nd October 2007, 19:41
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  5. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 18:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.