Results 1 to 5 of 5

Thread: QLocalSocket, QThread and Signals

  1. #1
    Join Date
    Jan 2012
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default QLocalSocket, QThread and Signals

    Hi,

    I am writing a server with QLocalSocket. I'm experienced with C# and now I have some questions about Qt. If I write a QLocalServer with

    Qt Code:
    1. Server::Server(QString servername, QObject *parent)
    2. : QObject(parent), m_clients(NULL)
    3. {
    4. m_server = new QLocalServer(this);
    5.  
    6. if (!m_server->listen(servername)) {
    7. qDebug() << "Not able to start the Server";
    8. }
    9.  
    10. connect(m_server, SIGNAL(newConnection()), this, SLOT(socket_new_connection()));
    11. }
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. void Server::socket_new_connection()
    2. {
    3. QLocalSocket *clientConnection = m_server->nextPendingConnection();
    To copy to clipboard, switch view to plain text mode 

    What about socket_new_connection()? The signal is emitted every time a new connection is available. There are a lot of signals available in QLocalServer and QLocalSocket, but if the network is busy, my UI will not respond, right?

    I would like to call socket_new_connection() in a seperate thread. In C# you can write a BackgroundWorker, which starts listening in a seperate Thread.

    How would I pass a new Connection to an seperate Thread in Qt?

    Maybe it's better to create a Worker Thread (inherited from QThread) for every socket connection?
    Last edited by StarShaper; 9th January 2012 at 21:22.

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

    Default Re: QLocalSocket, QThread and Signals

    Quote Originally Posted by StarShaper View Post
    There are a lot of signals available in QLocalServer and QLocalSocket, but if the network is busy, my UI will not respond, right?
    Why would it not respond?

    I would like to call socket_new_connection() in a seperate thread.
    There is no need to do that.

    Maybe it's better to create a Worker Thread (inherited from QThread) for every socket connection?
    No, there is no point in doing that since Qt handles networking asynchronously. If you had 10000 sockets open, I would agree you needed a couple of (10?) threads to handle all those sockets. Provided you could satitate them all with data.
    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
    Jan 2012
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLocalSocket, QThread and Signals

    Quote Originally Posted by wysota View Post
    No, there is no point in doing that since Qt handles networking asynchronously.
    Thanks for this information!

    Networking: all low-level Qt networking classes (QTcpSocket, QUdpSocket, QTcpServer, etc.) are asynchronous by design. When you call read(), they just return already available data; when you call write(), they schedule the writing for later. It’s only when you return to the event loop the actual reading/writing takes place. Notice that they do offer synchronous methods (the waitFor* family of methods), but their use is discouraged because they block the event loop while waiting. High-level classes, like QNetworkAccessManager, simply do not offer any synchronous API and require an event loop.
    Then I only need to perform the computing in a separate thread.

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

    Default Re: QLocalSocket, QThread and Signals

    Yes. I advise you to use QtConcurrent for this. Have a look at QRunnable and friends.
    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
    Jan 2012
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLocalSocket, QThread and Signals

    Thanks. I already found some nice Classes for my project, like QStateMachine.

Similar Threads

  1. qThread and Unix Signals
    By peterjb31 in forum Qt Programming
    Replies: 1
    Last Post: 7th April 2011, 11:24
  2. Signals not working in a QThread
    By curreli in forum Qt Programming
    Replies: 1
    Last Post: 10th August 2010, 16:04
  3. QThread SIGNALS
    By Max123 in forum Qt Programming
    Replies: 1
    Last Post: 29th March 2010, 13:17
  4. QThread and signals
    By bunjee in forum Qt Programming
    Replies: 8
    Last Post: 28th March 2008, 02:44
  5. QThread and signals (linux/UNIX signals not Qt Signals)
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 22:18

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
  •  
Qt is a trademark of The Qt Company.