PDA

View Full Version : socket notifiers cannot be enabled from another thread



babu198649
3rd April 2009, 16:45
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::DirectConnection);
connect(socket,SIGNAL(error(QAbstractSocket::Socke tError)),this,SLOT(displayError(QAbstractSocket::S ocketError)),
Qt::DirectConnection);//since QAbstractSocket is not a registeredMetaType(?) use Qt::DirectConnection

}

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.

ComaWhite
3rd April 2009, 17:13
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

^NyAw^
3rd April 2009, 17:26
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.

talk2amulya
3rd April 2009, 18:17
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

talk2amulya
3rd April 2009, 18:18
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

babu198649
4th April 2009, 16:15
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::DirectConnection 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).