Results 1 to 6 of 6

Thread: Problem with QTcpSocket and QThread

  1. #1
    Join Date
    May 2010
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Problem with QTcpSocket and QThread

    Hi, I try to send huge files to Internet by HTTP POST, but QNetworkAccessManager load whole file in memory before send it, and that's is a problem for huge files. So, I have trying to implement a program that send large files on chunks through QTcpSocket in a QThread in background. But, as they are large files, I want to give to user option to cancel operation.

    My problem is that when user cancel the operation, sometimes all work fine. But other sometimes, the socket continue sending file in background even though I've canceled. And sometimes, thread stop send data but the socket it remains open until whole program closes.

    I have read many posts of this forum, and others, talking about QThreads and QTcpSockets/QTimers, but I can't solve my problem. I'm desperate.

    The code is attached to this post.

    Thanks to all, and sorry for my poor english.
    Attached Files Attached Files

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QTcpSocket and QThread

    There is no need to use QThread to use QTcpSocket, you can do it in the main thread and remove all complexity included with multiple threads.

  3. #3
    Join Date
    May 2010
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Problem with QTcpSocket and QThread

    Ok. But, if I do that, GUI will freeze until file has been uploaded.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Problem with QTcpSocket and QThread

    Do not use blocking functions.
    When you do not need to send data synchronously, you do not need threads.

    Quoting the documentation:
    QAbstractSocket provides a set of functions that suspend the calling thread until certain signals are emitted. These functions can be used to implement blocking sockets:
    waitForConnected() blocks until a connection has been established.
    waitForReadyRead() blocks until new data is available for reading.
    waitForBytesWritten() blocks until one payload of data has been written to the socket.
    waitForDisconnected() blocks until the connection has closed.
    Do not use the above functions.
    Just call write to send your data

  5. #5
    Join Date
    May 2010
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Problem with QTcpSocket and QThread

    Quote Originally Posted by tbscope View Post
    Do not use blocking functions.
    When you do not need to send data synchronously, you do not need threads.

    Quoting the documentation:


    Do not use the above functions.
    Just call write to send your data
    As you can see in the code, I only use waitForReadyRead() when file was entirely uploaded. I think the problem isn't there.

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QTcpSocket and QThread

    You use waitForReadyRead(). Do not use this.

    You have a loop to send out all the data. Do not use this.

    Instead, use signals and slots. Eg. use bytesWritten signal to determine when you can send more data.

  7. The following user says thank you to squidge for this useful post:

    ivansjg (25th May 2010)

Similar Threads

  1. QTcpSocket in QThread
    By zverj in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2009, 09:27
  2. QTcpSocket in QThread
    By Alex Snet in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2009, 19:02
  3. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2008, 13:06
  4. Problem with QTcpSocket in QThread
    By Raistlin in forum Qt Programming
    Replies: 8
    Last Post: 6th October 2007, 12:23
  5. QThread and QTcpSocket problem
    By probine in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2006, 09:57

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
  •  
Qt is a trademark of The Qt Company.