Results 1 to 7 of 7

Thread: socket in thread error!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2007
    Posts
    8
    Thanks
    7

    Exclamation socket in thread error!

    Dear everybody!

    now I'm using socket in thread!

    in the thread:
    //////////////////

    if (!tcpSocket.setSocketDescriptor(socketDescriptor))
    {
    emit error(tcpSocket.error());
    return;
    }

    tcpSocket.waitForReadyRead();

    QByteArray Mark = tcpSocket.read(10);

    //////////////////
    when the program was ran the error is reported!

    cannot send events to objects owned by a different thread!

    when I remove the "tcpSocket.waitForReadyRead();",the error isn't appear,but I cannot get any data form socket!
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: socket in thread error!

    can you show more of your code?
    your signal slot connection lines, and your and your QThread subclass run().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    alphaboy (15th November 2007)

  4. #3
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: socket in thread error!

    No need to show the code, I recognize the error


    To get it to work, you are going to have to subclass QTcpSocket, and then in the run() method of the subclassed QThread create it, so that it lives only in that QThread.

    Put all the code that connects the signals and slots in the constructor of the subclassed QTcpSocket, and handle all readyReads() and such in the subclassed QTcpSocket as well.

  5. The following user says thank you to Valheru for this useful post:

    alphaboy (15th November 2007)

  6. #4
    Join Date
    Sep 2007
    Posts
    8
    Thanks
    7

    Default Re: socket in thread error!

    Thank you very much!

    I post the code now.my English is not good,so I can't understand very well.Can you show me an example?

    Here is my code!

    void socketThread::run()
    {

    if (!tcpSocket.setSocketDescriptor(socketDescriptor))
    {
    emit error(tcpSocket.error());
    return;
    }

    tcpSocket.waitForReadyRead();

    while(true)
    {

    if (tcpSocket.bytesAvailable()>0)
    {
    QByteArray Mark = tcpSocket.read(10);

    if (Mark == gc_Mark)
    {
    QByteArray CmdType =tcpSocket.read(1);

    switch (CmdType.at(0))
    {
    case Configure:
    parserSendConfig();
    break;
    case LogRequest:
    upCurrentLog();
    break;
    case TimeAdjust:
    setTime();
    break;
    default:
    ;
    }

    }
    }

    }

    }

  7. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: socket in thread error!

    There are two things that jump right away, so I'll start with those.
    It could be however you have other problems in there as well.

    The first thing is that you don't need to use an endless while loop, but can use the QTcpSocket readyRead() signal.
    This is not a problem on its on, just a cleaner way to do it.

    The other problem, is the thread affinity.
    Please read the docs about threading in Qt, specially this section about thread affinity.

    Basically, only objects allocated IN the run() method will have the thread class affinity (of the non GUI thread) - and your 'tcpSocket' object is allocated outside the run() function, in the main GUI thread, which is the reason you get the error you reported.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. The following user says thank you to high_flyer for this useful post:

    alphaboy (18th November 2007)

  9. #6
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: socket in thread error!

    Not only that, but if you want to use Qt's signals and slot mechanism, you HAVE to call exec() in the threads run() method otherwise the thread has no event loop. (Well, I suppose you could implement an event loop yourself but that yould be nasty ). So ditch the while loop and jsut call exec() in the thread's run() method. Handle all of the signal's emitted by the socket in socket.cpp (or whatever the name of your subclassed QTcpSocket is). Connect all the signals and slots in the constructor of your QTcpSocket subclass (or in the run() method of your thread, but personally I find that messy). I can't get any clearer than that

    If you want an example, see http://knewz.svn.sourceforge.net/vie...ewz/trunk/src/ Look at the classes connection.h/cpp and socket.h/cpp and see how I implemented this there.

  10. The following user says thank you to Valheru for this useful post:

    alphaboy (18th November 2007)

  11. #7
    Join Date
    Sep 2007
    Posts
    8
    Thanks
    7

    Default Re: socket in thread error!

    Tanks very much! the program is solved!

    I have learned how to use even in thread!



    Thanks everyone!

Similar Threads

  1. Error compiling psql plugin
    By vieraci in forum Installation and Deployment
    Replies: 4
    Last Post: 7th October 2007, 02:49
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  3. qt 4.2.2 install on aix
    By try to remember in forum Installation and Deployment
    Replies: 2
    Last Post: 28th March 2007, 12:19
  4. Qt-x11-commercial-src-4.2.0-snapshot-20060824 error
    By DevObject in forum Installation and Deployment
    Replies: 4
    Last Post: 24th August 2006, 23:31
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52

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.