PDA

View Full Version : Problem with QTcpSocket and QThread



ivansjg
19th May 2010, 11:46
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.

squidge
19th May 2010, 12:59
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.

ivansjg
19th May 2010, 13:06
Ok. But, if I do that, GUI will freeze until file has been uploaded.

tbscope
19th May 2010, 13:14
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

ivansjg
19th May 2010, 13:24
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.

squidge
19th May 2010, 14:53
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.