PDA

View Full Version : Socket thread problem



waynew
16th October 2011, 01:51
Yes, against the expert's advice, I am using a separate thread for QTcpSocket to send data to a server.
Here is why - it takes between 10 and 15 seconds to log into the server, receive a response, then send the data.
If a separate thread is not used, the gui is frozen during this time which is unacceptable.

The problem however is that when the data is sent to the server, and the send loop exited, and the signal sent back to the originating dialog to close, the slot in the dialog to close the operation never executes. So I am assuming the signal is never received. Using debug, I can see that the process is hung up in a looping nature having to do with mutex when the send loop in the thread is exited and the signal is sent. This is puzzling since there are no shared objects between this thread and the main thread.

I did read "you are doing it wrong". Does this imply that simply moving the networking code to a new class and calling it from the thread class will solve the problem? It doesn't seem like it. Yes, I did try setting the connection as queued. No difference.

Suggestions are welcome and thanks!

wysota
16th October 2011, 17:55
Yes, against the expert's advice, I am using a separate thread for QTcpSocket to send data to a server.
Here is why - it takes between 10 and 15 seconds to log into the server, receive a response, then send the data.
If a separate thread is not used, the gui is frozen during this time which is unacceptable.
Then use asynchronous API, nothing will block then.

waynew
16th October 2011, 20:17
I'm not sure how to do this Wysota. What would the structure look like? How can you be listening for the server response and still running the gui?

wysota
16th October 2011, 21:01
You would connect to your socket's readyRead() signal or other signals appropriate to the situation, as the examples bundled with Qt show.

waynew
17th October 2011, 01:26
Thanks Wysota - I think I see what you mean. I am going to restructure this way and see how it works.
I'm sure you will have some sage advice if I have problems :)