Results 1 to 3 of 3

Thread: Threaded Sever Problem

  1. #1
    Join Date
    Jan 2006
    Posts
    33
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11

    Question Threaded Sever Problem

    Hi there,

    I playing around with an threaded server. I had a look at the threaded fortune example which was a good start.
    The different result I'm after is a worker which doesn't only returns a string but stays alive for longer.
    To make life even more complicate I have a resource which can't be shared. Lets say the response a client gets is dependent on the state of this resource and this state may change from the requests it receives from the connected clients.
    I have created some sort of sequence diagram of what I like to achieve.
    I also attach the complete code (it's a small example) which can be build and run with `qmake && make && ./server`
    The server will listen on port 6446 but this can be changed in the source code.
    In case the port is not changed you may connect with `telnet localhost 6446`.
    Qt Code:
    1. >telnet localhost 6446
    2. Trying 127.0.0.1...
    3. Connected to localhost.
    4. Escape character is '^]'.
    5. soso
    6. Server received: soso
    7.  
    8. salamander
    9. Server received: salamander
    To copy to clipboard, switch view to plain text mode 

    Now the part I don't understand

    Qt Code:
    1. #:~/projects/Qt4/server> ./server
    2. new connection id: 8
    3. Worker running
    4. QNativeSocketEngine::
    5. received data
    6. going to create response!
    7. send answer
    8. flushed socket
    9. ################## ! ! ! ! #####################
    10. QObject: Cannot create children for a parent that is in a different thread.
    11. QSocketNotifier: socket notifiers cannot be disabled from another thread
    12. #^#^#^#^############## ! ! ! ! #################
    13. new connection id: 12
    14. Worker running
    15. QNativeSocketEngine::
    16. received data
    17. going to create response!
    18. send answer
    19. flushed socket
    20. ################## ! ! ! ! #####################
    21. QObject: Cannot create children for a parent that is in a different thread.
    22. QSocketNotifier: socket notifiers cannot be disabled from another thread
    23. #^#^#^#^############## ! ! ! ! #################
    24. received data
    25. going to create response!
    26. send answer
    27. flushed socket
    To copy to clipboard, switch view to plain text mode 

    After quite a bit of tinkering about I found out where this QObject warning comes from.
    It doesn't really help me though and about that SocketNotifier error I'm complete lost.

    Qt Code:
    1. gdb - output
    2. Breakpoint 2, QObject (this=0x8059260, parent=0x8056ca8) at qobject.cpp:619
    3. 619 qWarning("QObject: Cannot create children for a parent that is in a different thread.");
    4. (gdb) backtrace
    5. #0 QObject (this=0x8059260, parent=0x8056ca8) at qobject.cpp:619
    6. #1 0x4022ffaa in QSocketNotifier (this=0x8059260, socket=12, type=Write, parent=0x8056ca8) at qsocketnotifier.cpp:124
    7. #2 0x400978e3 in QNativeSocketEngine::setWriteNotificationEnabled (this=0x8056ca8, enable=true)
    8. at qnativesocketengine.cpp:915
    9. #3 0x400a5681 in QAbstractSocket::writeData (this=0x8058f08, data=0x805a3e0 "Server reseived: soso\r\n\n", size=24)
    10. at qabstractsocket.cpp:1761
    11. #4 0x401ae99b in QIODevice::write (this=0x8058f08, data=0x805a3e0 "Server reseived: soso\r\n\n", maxSize=24)
    12. at qiodevice.cpp:1241
    13. #5 0x40081969 in QIODevice::write (this=0x8058f08, data=@0xbfffe950) at qiodevice.h:105
    14. #6 0x401b5438 in QTextStreamPrivate::flushWriteBuffer (this=0x8059298) at qtextstream.cpp:617
    15. #7 0x401b66c1 in ~QTextStream (this=0xbfffe9c0) at qtextstream.cpp:1019
    16. #8 0x0804b115 in Worker::respond (this=0x8057ce8) at worker.cpp:82
    17. #9 0x0804ae92 in Worker::bufferData (this=0x8057ce8) at worker.cpp:59
    18. #10 0x0804b384 in Worker::qt_metacall (this=0x8057ce8, _c=InvokeMetaMethod, _id=0, _a=0x805a430) at moc_worker.cpp:66
    19. #11 0x40223e57 in QObject::event (this=0x8057ce8, e=0x805a1d8) at qobject.cpp:1022
    20. #12 0x40210a72 in QCoreApplicationPrivate::notify_helper (this=0x804e130, receiver=0x8057ce8, event=0x805a1d8)
    21. at qcoreapplication.cpp:613
    22. #13 0x40210881 in QCoreApplication::notify (this=0xbfffef30, receiver=0x8057ce8, event=0x805a1d8)
    23. at qcoreapplication.cpp:571
    24. #14 0x402148cb in QCoreApplication::sendEvent (receiver=0x8057ce8, event=0x805a1d8) at qcoreapplication.h:183
    25. #15 0x402114e6 in QCoreApplication::sendPostedEvents (receiver=0x0, event_type=0) at qcoreapplication.cpp:1022
    26. #16 0x4023e1f5 in postEventSourceDispatch (s=0x8056968) at qeventdispatcher_glib.cpp:250
    27. #17 0x4050ffc7 in g_main_context_dispatch () from /opt/gnome/lib/libglib-2.0.so.0
    28. #18 0x40512392 in g_main_context_acquire () from /opt/gnome/lib/libglib-2.0.so.0
    29. #19 0x405124a3 in g_main_context_iteration () from /opt/gnome/lib/libglib-2.0.so.0
    30. #20 0x4023f183 in QEventDispatcherGlib::processEvents (this=0x8055800, flags={i = 20}) at qeventdispatcher_glib.cpp:363
    31. ---Type <return> to continue, or q <return> to quit---
    32. #21 0x4020df81 in QEventLoop::processEvents (this=0xbfffeed0, flags={i = 20}) at qeventloop.cpp:125
    33. #22 0x4020e06d in QEventLoop::exec (this=0xbfffeed0, flags={i = 0}) at qeventloop.cpp:171
    34. #23 0x40210cda in QCoreApplication::exec () at qcoreapplication.cpp:720
    35. #24 0x08049f85 in main (argc=1, argv=0xbfffefd4) at main.cpp:11
    To copy to clipboard, switch view to plain text mode 

    It looks like the warning is issued when a new SocketNotifier is to be created.

    My wild guess is that it has something to do with the way and where I'm creating the QTcpSocket and the QTextStream using it.
    But at the moment I'm complete glueless and would appreciate very much if someone could shed some light on that for me.

    Thanks!

    Moppel

    Below the code which I believe is most relevant. But the complete code is attached.
    Qt Code:
    1. void Server::incomingConnection(int socketId){
    2. qDebug() << "new connection id: " << socketId;
    3. Worker *wrk = new Worker(this, socketId);
    4. ((QThread*)wrk)->start();
    5. }
    6.  
    7. void Worker::run(){
    8. qDebug() << "Worker running";
    9. _socket = new QTcpSocket();
    10. // _socket = new QTcpSocket(currentThread()); // Fails with
    11. // "Cannot create children for a parent that is in a different thread." --- Why??
    12.  
    13. _socket->setSocketDescriptor(_socketDescriptor);
    14. _socket->dumpObjectTree();
    15.  
    16. connect(_socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
    17. connect(_socket, SIGNAL(readyRead()), this, SLOT(bufferData()));
    18.  
    19. forever{
    20. QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
    21. }
    22. }
    23.  
    24. void Worker::respond(){
    25. qDebug() << "going to create response!";
    26. QByteArray response = dad->getResourceInstance()->createResponse(buffer);
    27. dad->releaseResourceInstance();
    28.  
    29. QTextStream os(_socket);
    30. os << response << "\n";
    31. qDebug() << "send answer";
    32. _socket->flush();
    33. qDebug() << "flushed socket";
    34. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Last edited by jacek; 12th November 2006 at 22:36. Reason: removed attachment

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threaded Sever Problem

    Quote Originally Posted by Moppel View Post
    ((QThread*)wrk)->start();
    Simple "wrk->start()" will do.

    Quote Originally Posted by Moppel View Post
    // _socket = new QTcpSocket(currentThread()); // Fails with
    // "Cannot create children for a parent that is in a different thread." --- Why??
    Because "current thread" object lives in a different thread. It cannot live in the newly created worker thread, because it was created before that new thread was started.

    Quote Originally Posted by Moppel View Post
    connect(_socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)) , this, SLOT(socketStateChanged(QAbstractSocket::SocketSta te)));
    connect(_socket, SIGNAL(readyRead()), this, SLOT(bufferData()));
    Add Qt::DirectConnection, otherwise those connections will be queued and all of the work will be done be the GUI thread, since "this" lives there.

    Quote Originally Posted by Moppel View Post
    forever{
    QCoreApplication::processEvents(QEventLoop::WaitFo rMoreEvents);
    }
    This will process events of the GUI thread, change it to:
    Qt Code:
    1. exec();
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to jacek for this useful post:

    Moppel (12th November 2006)

  4. #3
    Join Date
    Jan 2006
    Posts
    33
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11

    Thumbs up Re: Threaded Server Problem [solved]

    Thanks jaceck!

    It works!
    I must admit I havn't fully understood it yet, so I better will read again
    http://doc.trolltech.com/4.2/threads.html
    and play around it a bit more.

    Just for reference I'll attach the working sources.

    Cheers Moppel

    P.S. I would have liked to delete the old sources but somehow I can't do this. I can't even edit my previous posting.
    Attached Files Attached Files

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.