PDA

View Full Version : Threads communication



probine
30th March 2006, 12:59
My program is a server and it has two clients connected to it. Each client connected to the server is a separate thread.

I need somehow to be able to send a message from clientA to clientB and vise-versa, remember clientA and clientB are different threads.

How could I do this ?

wysota
30th March 2006, 13:16
Any combination of shared memory, shared variable, message queue, custom event (including signal/slot call) will be fine. Just remember to do some synchronisation of threads when appropriate.

jpn
30th March 2006, 13:27
You can emit signals between threads (a queued connection).

You could emit a signal from a client thread:


signals:
void messageArrived(const QString& message);


emit messageArrived("blaa");

Then you have at least 2 ways to deliver the message for other clients:
1) Create a connection between the server object (which creates and launches client threads) and all the clients. Pass the arrived message to all interested clients.
2) Create connections between all client threads, so when a client thread emits an arrived message, all other clients receive it directly..

probine
31st March 2006, 13:44
I am interested in doing this:

A client connects to the server and the server creates a thread that will handle the client.

Assume that the server has 3 clients connected, therefore 3 threads ( 1 for each client ".

Client A wants to send message to client C, but client B shouldn't know anything about it.

How ??? Post some code if possible.

I'm sorry, but I am totally confused.

wysota
31st March 2006, 14:46
Did you try something by yourself? The easiest way would be to send a signal to the "main" thread which would then send a signal/custom event/call a method of one of the threads.

If we give you a ready code, you won't learn anything and will be as confused as you were before.