PDA

View Full Version : Threaded QTcpServer + Send message to all connected clients



NoRulez
8th October 2009, 10:14
Hi @all,

as a base I used the "Threaded Fortune Server" example. But i don't know where I must save the sockets of the connected clients, because the socket is created within the thread...
so the server didn't have access to these sockets.

My plan is to send a message to all connected clients if the server receives a special message or a date/time is reached.

Could someone please help me
Thanks in advance

Best Regards
NoRulez

faldzip
8th October 2009, 11:15
in main thread create some signal - let's call it broadcast signal - with a message as an argument, and make appropriate slot in your QThread subclass and connect every new thread to this one signal. In slot just send this message through the socket. So when you emit signal, every socket should send the message.

daemonna
28th September 2010, 02:56
broadcast to all clients is pretty straightforward, but how about sending message to certain group of clients.... let's say:

- i have multithreaded server with 'rooms'
- only certain number of clients can connect to specific room (let's say 10)
- room must be able to send message to all 10 clients, but only to them... clients in other rooms are unaffected

only idea i got is
- room got 'message qeue' witch every client will check once a while
- room got id of connected clients (socket descriptor maybe) and will pass message to every client

do you think there are other options? if not, to which option from above i should stick?

tbscope
28th September 2010, 07:40
First, don't use threading:
Multi client server without threading (http://www.qtcentre.org/wiki/index.php?title=Multi_client_server_without_thread ing)

If you only use a single server (not peer to peer), then the structure is very simple. Use basic object oriented techniques to create structures for rooms, clients, etc...

Create a basic communication structure, or just use xml to communicate between clients.

David_G_Ortega
30th November 2010, 22:09
You should use a Observer pattern for this work

http://en.wikipedia.org/wiki/Observer_pattern

Also you can do a static property in your class that can be accesed to update all the instances.