Results 1 to 16 of 16

Thread: Networking Threading Issue

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2011
    Location
    Santa Clara, CA
    Posts
    13
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Networking Threading Issue

    Okay, so I'm having trouble with trying to get networking and threading to play nice together. Without holding up the GUI thread I'm trying to launch a new thread, and on that new thread communicate with a client and do intensive operations.

    Originally I had the GUI make a new thread (I made a class which sublcassed QThread) but I kept getting runtime warnings upon write() because I guess the socket is tied to the server which is tied to the original GUI thread, so I was getting an error similar (but with different classes) to this: QObject: Cannot create children for a parent that is in a different thread.
    (Parent is server_thread(0x6125e60), parent's thread is QThread(0x282730), current thread is server_thread(0x6125e60)

    So I reworked it using the QT fortune server example and I'm still having issues (now I get the warning and it doesn't work at all), if somebody could point me to another example I can use for reference I would appreciate it, my creation is like this:
    In GUI I launch the server class with:
    Qt Code:
    1. server = new rtserver(this);
    2.  
    3. server->listen(QHostAddress::AnyIPv4, quint16(PORT_NUMBER));
    To copy to clipboard, switch view to plain text mode 

    rtserver subclasses QTcpServer, in there I overrode the incomingConnection() function, and I create the thread in there like this:
    Qt Code:
    1. st = new server_thread(mw, socket_descriptor, this);
    2. st->start();
    To copy to clipboard, switch view to plain text mode 

    Where MW is just a pointer to the MainWindow I need for the thread to do some calculations.

    Then in the server thread which subclasses QThread I simply set the socket descriptor and MainWindow pointer in the c'tor, in run() I have:
    Qt Code:
    1. mClientSocket = new QTcpSocket(this);
    2.  
    3. (mClientSocket->setSocketDescriptor(mSocketDescriptor)
    To copy to clipboard, switch view to plain text mode 

    If I run it like this, I get the error above upon socket creation and the socket never receives any data.

    If I don't pass "this" as a parent to QTcpSocket then simply this always returns 0 (error is gone):
    Qt Code:
    1. bytes_avail = mClientSocket->bytesAvailable();
    To copy to clipboard, switch view to plain text mode 

    Should I or shouldn't I be passing the server thread as a parent to QTcpSocket in my thread? I feel like I should but in the Qt Fortune example they do not.

    I've tested the networking code successfully with Qt (and it works with the old method, it responds to the client), but I was getting this parent runtime warning about accessing the socket from a different thread from the Qt library upon calling write() function, so I'm trying to fix that....and I can't!!!

    I feel like I'm not setting up the socket correctly anymore, the previous method relied on the GUI calling "nextPendingConnection()" but with the new method that doesn't get called anymore (incomingConnection() is overridden and it just spawns a new thread and starts it, the GUI thread returns after that). I don't think there's any additional code I need in the incomingConnection() override, the fortune example doesn't have any, just makes the thread and starts it, passing in the raw socket descriptor...but this whole 'passing the raw socket descriptor' bit doesn't seem to be working in the new method..

    Any advice?

    NOTE: Fortune server example I mention: http://qt-project.org/doc/qt-5/qtnet...r-example.html
    Last edited by Syndacate; 14th October 2014 at 11:05.

  2. #2
    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: Networking Threading Issue

    Do you call exec() in your run() method?

    I.e. do you start the thread's event loop?

    Cheers,
    _

  3. #3
    Join Date
    Jul 2011
    Location
    Santa Clara, CA
    Posts
    13
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Networking Threading Issue

    Quote Originally Posted by anda_skoa View Post
    Do you call exec() in your run() method?

    I.e. do you start the thread's event loop?

    Cheers,
    _
    Thanks for your response.

    The only exec() I call is when I exec the app from the main file.

    The server_thread subclasses QThread, so after I construct it in the rtserver, I call:
    Qt Code:
    1. st->start()
    To copy to clipboard, switch view to plain text mode 

    I expected start() to exec the thread, no?

    The run() function (which in this case is just an event loop) of the server_thread gets called. The first thing it does is use the raw socket descriptor it got in the c'tor to construct a QTcpSocket in itself (so it's part of the new thread), then it goes into an event loop handling requests from client, doing calculations, and writing data back to them. It is most definitely calling run() because the socket gets constructed (which is when it gives the warning now) and printing stuff out, etc. It's just not getting data from the socket anymore.

    Since I overrode incomingConnection() in the server, I just pass the raw/native socket descriptor to the new thread and the new thread constructs the QTcpSocket around it...but the read/write doesn't seem to be working anymore and I'm not sure why... The old method called nextPendingConnection() in the original thread to get the QTcpSocket...then I passed that socket over to the server thread. Qt complained about this on the write() function saying that I shouldn't be writing to a QTcpSocket that is on a different thread, so I restructured it like this so that the QTcpSocket is constructed on the spawned thread.
    Last edited by Syndacate; 14th October 2014 at 18:35.

  4. #4
    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: Networking Threading Issue

    Quote Originally Posted by Syndacate View Post
    The only exec() I call is when I exec the app from the main file.
    Then you are missing it in the thread's run()

    Quote Originally Posted by Syndacate View Post
    The server_thread subclasses QThread, so after I construct it in the rtserver, I call:
    Qt Code:
    1. st->start()
    To copy to clipboard, switch view to plain text mode 

    I expected start() to exec the thread, no?
    That starts the thread. The default implementation of run() calls exec() to start the thread's event loop.
    Since you overwrote that, you have to start it either by calling the base implementation of run() or calling exec() yourself.

    If you don't execute the thread's event loop then the thread will exit as soon as the run method's scope is done.

    Cheers,
    _

Similar Threads

  1. Classic GUI and threading issue
    By stef13013 in forum Qt Programming
    Replies: 4
    Last Post: 3rd August 2012, 17:43
  2. Classic GUI and threading issue
    By stef13013 in forum Qt Programming
    Replies: 2
    Last Post: 3rd August 2012, 15:59
  3. Threading issue in DLL
    By CristonDK in forum Qt Programming
    Replies: 6
    Last Post: 15th June 2012, 18:07
  4. Threading Issue
    By noufalk in forum Qt Programming
    Replies: 4
    Last Post: 4th August 2007, 13:45
  5. Qt-3 and networking
    By a550ee in forum Qt Programming
    Replies: 3
    Last Post: 3rd October 2006, 11:15

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.