Results 1 to 5 of 5

Thread: Threaded QTcpServer + Send message to all connected clients

  1. #1
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Threaded QTcpServer + Send message to all connected clients

    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

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Threaded QTcpServer + Send message to all connected clients

    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.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    May 2010
    Location
    slovakia
    Posts
    47
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo

    Lightbulb Re: Threaded QTcpServer + Send message to all connected clients

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Threaded QTcpServer + Send message to all connected clients

    First, don't use threading:
    Multi client server without threading

    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.

  5. #5
    Join Date
    Aug 2010
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Threaded QTcpServer + Send message to all connected clients

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.