Results 1 to 7 of 7

Thread: QWebSocketServer howto move new connections to a own thread

  1. #1
    Join Date
    Mar 2013
    Posts
    29
    Thanks
    4
    Thanked 4 Times in 2 Posts

    Default QWebSocketServer howto move new connections to a own thread

    Hi,

    I’m developing a client-server application. Therefore I’m using the QWebSocket class to achieve binary transparency and bidirectional data transfer. To listen for incoming connections I’m using the QWebSocketServer class which works pretty nice so far. (basically the echoclient and echoserver example)

    Now my question:
    I would like to have a thread for every incoming connection (the QTcpServer class does support this) but the documentation says the returned QWebSocket* pointer by nextPendingConnection() cannot be used from another thead.
    Is this somehow possible and how?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QWebSocketServer howto move new connections to a own thread

    Quote Originally Posted by M4chin3 View Post
    I would like to have a thread for every incoming connection
    Bad idea. I could crash your app in seconds.

    Is this somehow possible and how?
    If the docs says it can't be used from another thread then it can't. If you really want to, you can make a proxy that will talk to the socket over cross-thread signals and slots. However I really can't understand why so many people try to force using threads for network communication. There is no blocking operation here, so there is simply no benefit from using threads and there are usually many problems with thread synchronization.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2013
    Posts
    29
    Thanks
    4
    Thanked 4 Times in 2 Posts

    Default Re: QWebSocketServer howto move new connections to a own thread

    Quote Originally Posted by wysota View Post
    Bad idea. I could crash your app in seconds.
    Why?

    Quote Originally Posted by wysota View Post
    If the docs says it can't be used from another thread then it can't......
    The same thing is mentioned for QTcpServer for the function QTcpServer::nextPendingConnection but it is possible to overwrite QTcpServer::incomingConnection for this.
    I just wanted to know if there is a possiblity to do it.

    Why threads? -> Maybe every connection can do a blocking operation in my application and therefore I want to decouple them from the rest of the application/listener.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QWebSocketServer howto move new connections to a own thread

    Quote Originally Posted by M4chin3 View Post
    Why?
    Because usually the number of threads you can spawn per process or per user is limited. By opening a large number of concurrent connections I'd either force your app to run out of threads or starve it by flooding it with context switching.

    Why threads? -> Maybe every connection can do a blocking operation in my application and therefore I want to decouple them from the rest of the application/listener.
    Then you want to perform that blocking operation in another thread (or better yet using a limited thread pool) and not push the whole communication there. You can decouple whatever you want decoupled by encapsulating it in an object. You don't require threads for that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2013
    Posts
    29
    Thanks
    4
    Thanked 4 Times in 2 Posts

    Default Re: QWebSocketServer howto move new connections to a own thread

    By decoupling I meant moving the task to another thread to avoid blocking the whole program. But basically you're right, I can move the blocking tasks to threads and keep the connections all together within another thread. I'm not worrying about the number of threads shouldn't be so many, but I'm giving the limited thread pool a thought.

    Thanks!

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QWebSocketServer howto move new connections to a own thread

    Quote Originally Posted by M4chin3 View Post
    I'm not worrying about the number of threads shouldn't be so many
    Right... and 640kB or RAM should be enough for anybody
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QWebSocketServer howto move new connections to a own thread

    Quote Originally Posted by M4chin3 View Post
    The same thing is mentioned for QTcpServer for the function QTcpServer::nextPendingConnection but it is possible to overwrite QTcpServer::incomingConnection for this.
    I think that part of the docs is actually outdated. Somewhen during Qt5 it became possible to move QTcpSocket instances to different threads.

    Cheers,
    _

Similar Threads

  1. Replies: 11
    Last Post: 12th September 2012, 16:25
  2. Replies: 1
    Last Post: 4th September 2012, 14:13
  3. multithread thread don't move
    By lzpmail in forum Qt Programming
    Replies: 11
    Last Post: 28th March 2011, 09:09
  4. Replies: 1
    Last Post: 30th July 2010, 07:23
  5. howto Processing GUI events until a thread exits
    By doggrant in forum Qt Programming
    Replies: 0
    Last Post: 5th October 2009, 15:50

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.