Results 1 to 6 of 6

Thread: socket notifiers cannot be enabled from another thread

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default socket notifiers cannot be enabled from another thread

    Hi
    I have a program which had GUI Thread and a working Thread for socket communication. I need to send the data from the GUI Thread (on mouse Click) to the socket. I get "socket notifiers cannot be enabled from another thread" warning and some times the app crashes.

    I can not use the Blocking approach for sockets since i need event of the sockets.The code is below

    void Network::run()
    {
    if(socket == 0)
    {
    socket = new QTcpSocket;
    connect(socket,SIGNAL(readyRead()),this,SLOT(readS ocket()),
    Qt:irectConnection);
    connect(socket,SIGNAL(error(QAbstractSocket::Socke tError)),this,SLOT(displayError(QAbstractSocket::S ocketError)),
    Qt:irectConnection);//since QAbstractSocket is not a registeredMetaType(?) use Qt:irectConnection

    }

    if(socket->state() == QAbstractSocket::ConnectedState)
    socket->disconnectFromHost();
    if(socket->state() != QAbstractSocket::UnconnectedState)
    socket->waitForDisconnected(-1);

    socket->connectToHost(ip,port);
    stopConnect = false;

    while(!socket->waitForConnected(3000))
    {
    if(stopConnect)
    {
    return;
    }
    qDebug()<<socket->state()<<socket->error()<<socket->errorString();
    }

    exec();
    }

    void Network::setAddress(QString ip_,qint16 port_)
    {
    ip = ip_;
    port = port_;

    stopConnect = true;//TODO-Eliminate this bool from program
    if(isRunning())
    {
    quit();
    wait();
    }
    start();
    }

    void Network::write2socket(QString str)
    {
    if( (socket == 0) || (!socket->isWritable()) )
    return;
    socket->write(str.toAscii());
    socket->flush();
    }
    when i call the write2socket from the gui thread i get the "socket notifiers cannot be enabled from another thread" warning and some times the app crashes.

    i have even tried using signals from the gui thread to call write2socket() ,But nothing has changed .

    Thanks in advance.

  2. #2
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: socket notifiers cannot be enabled from another thread

    You shouldn't be using waitFor* functions in GUI applications. And there should really be no use for thread socketing since its event based async
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: socket notifiers cannot be enabled from another thread

    Hi,

    First of all, read about threads. You are redefining the "run" method to finally call "exec", why? Do you really need a thread?
    The other problem I think that you are trying to connect the socket SIGNALs to the thread SLOTs in "DirectConnection" that is not possible as you will have two threads that have direct communication.
    Òscar Llarch i Galán

  4. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: socket notifiers cannot be enabled from another thread

    u r trying to call a write function on a socket created in another thread, from a different thread..Qt doesnt allow this..when u create a QTcpSocket..a QSocketNotifier object is created which is used for reading, writing and exceptions..this socket notifier is then registered with the event dispatcher..now the event dispatchers of both threads are different..thus it doesnt get registered and a warning is thrown and smtimes it gets crashed

  5. #5
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: socket notifiers cannot be enabled from another thread

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    First of all, read about threads. You are redefining the "run" method to finally call "exec", why?
    the exec function is used to start another event loop, thats not where he is wrong

  6. #6
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: socket notifiers cannot be enabled from another thread

    Thanks for your replys,

    You shouldn't be using waitFor* functions in GUI applications
    yes. But network class is a subclass of QThread.

    The other problem I think that you are trying to connect the socket SIGNALs to the thread SLOTs in "DirectConnection" that is not possible as you will have two threads that have direct communication.
    If i make this connection as Qt::QueuedConnection then the slots get executed in the gui thread and hence i used Qt:irectConnection and there is no problem with it.

    u r trying to call a write function on a socket created in another thread, from a different thread..Qt doesnt allow this
    Is there a work around to do this .Because i need to send data from the gui Thread(on clicked) and also i need to update the gui when i get the data from socket(readyRead signal).
    Last edited by babu198649; 4th April 2009 at 15:21.

Similar Threads

  1. thread with socket?
    By triperzonak in forum Qt Programming
    Replies: 6
    Last Post: 25th September 2008, 16:21
  2. Thread, Timer and Socket. Comuication problem
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2008, 16:48
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. Replies: 10
    Last Post: 20th March 2007, 22:19
  5. How to write on a socket in another thread?
    By Valheru in forum Qt Programming
    Replies: 7
    Last Post: 12th October 2006, 10: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.