PDA

View Full Version : QThread and QTcpSocket



^NyAw^
13th May 2008, 11:27
Hi,

If I use a socket into a QThread, and the "run()" method can lock itself depending on a QWaitCondition, the socket will continue reciving the network data until the thread is locked?

I want a QThread that sends messages(depending on the app state, user interaction, ...), but the QThread must not send any message until recives the confirmation of the previous message:

QThread(with QTcpSocket) Client
|-->send message------------------------------------------------>recive message
|
| locked untiil recive message (doing some jobs)
|
|--- unlock thread<-------------------------------------------------send confirmation


Thanks,

pokey909
19th May 2008, 17:13
Well, I'm not sure if i really know what you mean. But it looks like you want the thread to send a message through the socket and have it block until it receives a reply from the network client (or gui?).

First of all, why do you have to use a thread for this? It is not doing anything useful except sleeping and sending a message once it gets a reply. You could do it easily using the normal signal/slots with a non-threaded class. In case you didnt provide the full info and it does do something instead of waiting there are 2 solutions:

1. just give your thread an event-loop with the exec() function in your run method and connect the readyRead() signal of the socket to a slot in your thread. This is the non-blocking way.
2. in your run() method do something like:

forever {
socket->write(message);
socket->waitForBytesWritten();
socket->waitForReadyRead();
read_reply();
}


Can you maybe further explain why you need this thread or what it really does? Because i really think you dont need a thread for your task. Threads are a pain anyway, so evade them whenever possible! Just my 2 cents ;-)

^NyAw^
19th May 2008, 17:43
Hi,

Thanks for reply but I had solved yet.



First of all, why do you have to use a thread for this?


Because there are other Threads that are creting messages and them send the message to the communication thread that have to send it.

But don't worry about it, as I said, I solved it.

Thanks,