Results 1 to 4 of 4

Thread: sending a file(txt,docs,exe,rar,zip...) through QTcpSocket

  1. #1
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default sending a file(txt,docs,exe,rar,zip...) through QTcpSocket

    Hello friends,
    Im really knew to this forum,
    i have been trying to write a Qt c++ code for transferring files from client to server over a QTcpServer
    i have done some long work and different functions for the whole program including the GUI
    but now i wanna implement the download button, where the client has a list of the server files , upon double clicking the client will
    download the file from server to his PC.
    i tried several ways but non is working out for me
    i know i should read the file into chunks and feed the socket the chunk while looping till there is no bytes available
    can anyone help
    give me a sample code of this or another way or any clue
    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: sending a file(txt,docs,exe,rar,zip...) through QTcpSocket

    Both QFile and QTcpSocket are subclasses of QIODevice, so transferring bytes from the file to the socket is only a matter of finding matching read and write functions.

    For example there is QIODevice::read( qint64 ) which returns a block of data as a QByteArray. The matching write function is QIODevice::write( QByteArray ) which takes such a data block and writes it.

    Now one thing to consider of course is that the socket needs to transfer data to an "endpoint" that can be considerably slower than a file.
    In a situation with blocking I/O one could call QIODevice::waitForBytesWritten() after each write, making sure the data has been sent before calling read() on the file again.
    In a situation with non-blocking I/O one would connect to the QIODevice::bytesWritten( int ) signal and only attempt another read/write cycle when all or most data from the previous write has been sent.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    toufic.dbouk (29th December 2012)

  4. #3
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: sending a file(txt,docs,exe,rar,zip...) through QTcpSocket

    Thanks for your reply Admin anda_skoa.
    im not really familiar with QIODevice, may you please show me a sample code that is supposed to send a file of any type to a server ?
    That would be really helpful and ill take a look at the qt reference documentation of QIODevice class to well understand it
    Thanks again,

  5. #4
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: sending a file(txt,docs,exe,rar,zip...) through QTcpSocket

    there are plenty of network examples on the the digia help site.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 7
    Last Post: 12th September 2011, 10:52
  2. QTcpSocket get disconnected when server is sending huge data
    By KernelCoder in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2011, 08:45
  3. Replies: 6
    Last Post: 22nd December 2010, 03:20
  4. Sending mp3 files over QTcpSocket
    By ada10 in forum Newbie
    Replies: 5
    Last Post: 17th August 2010, 08:05
  5. Packet getting fragmented when sending over QTcpSocket
    By arturo182 in forum Qt Programming
    Replies: 14
    Last Post: 5th August 2009, 23:11

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.