Socket recvied and signal and slot
-
Dear all,
I wrote the following signal that it can specify socket has data or not:
@bool NetworkSocket::isDataReady()
{/// Got here because iSelectReturn > 0 thus data available on at least one descriptor // Is our socket in the return list of readable sockets bool res; fd_set sready; struct timeval nowait; FD_ZERO(&sready); FD_SET((unsigned int)this->socketFD,&sready); //bzero((char *)&nowait,sizeof(nowait)); memset((char *)&nowait,0,sizeof(nowait)); res = select(this->socketFD+1,&sready,NULL,NULL,&nowait); if( FD_ISSET(this->socketFD,&sready) ) res = true; else res = false; if (res) emit createThread( ); return res;
}@
Then i wrote a slot that it work standalone and create thread:
@bool NetworkSocket::createThread()
{bool returnValue = true ; void * args = NULL; pthread_t tempID; if (!firstTime) { /*(void*(*)(void*))*/
/* if (pthread_create(&tempID, NULL,(void * ()(void )) &threadProcedure , (void )this) != 0)
{
//error
}
this->threadCounter++;
this->threadVector.push_back(tempID);
this->firstTime = false;/
}
else
{
/(void()(void))/
if (pthread_create(&tempID, NULL, (void()(void)) &NetworkSocket::threadProcedure , reinterpret_cast<void *>(this)) != 0)
{
//error
}this->threadVector.push_back(tempID); ++this->threadCounter; } returnValue = false; return returnValue;
}@
I put in theradProcudre a cout, and the following line in contructor:
@QObject::connect(this,SIGNAL(isDataReady()),this,SLOT(createThread( )));@
But my slot doesn't run.. what's happen? -
Did you subclass QObject?
And you should call the method isDataReady(). -
my class definition:
@class NetworkSocket : public QObject{
Q_OBject
signals:
inline bool isDataReady();
public slots:
bool createThread();};
@ -
Hi,
You should use Q_OBJECT not Q_OBject (but this error for sure is catched at compilation time).
Next, I do not see in the code where you call@
emit isDataReady();
@Maybe this is a problem?
-
...and Signals shouldn't have an implementation or return value!
As the name says: its just a signal, that somebody else can react to... -
in your code you are emiting createThread( ) signal using @emit createThread( );@
This doesn't call the method createThread( ) but tries to emit a signal named createThread( ) which you don't have. That is why it not working.I thin you should have this
@
class NetworkSocket : public QObject{
Q_OBject
public:
bool isDataReady();
signals:
void dataReady();
public slots:
bool createThread();};
@
@
bool NetworkSocket::isDataReady()
{/// Got here because iSelectReturn > 0 thus data available on at least one descriptor // Is our socket in the return list of readable sockets bool res; fd_set sready; struct timeval nowait; FD_ZERO(&sready); FD_SET((unsigned int)this->socketFD,&sready); //bzero((char *)&nowait,sizeof(nowait)); memset((char *)&nowait,0,sizeof(nowait)); res = select(this->socketFD+1,&sready,NULL,NULL,&nowait); if( FD_ISSET(this->socketFD,&sready) ) res = true; else res = false; if (res) emit dataReady( ); return res; }
@
@
QObject::connect(this,SIGNAL(dataReady()),this,SLOT(createThread( )));
@ -
What about using QTcpSocket, QTcpServer or similar?
-
what do you mean ?
In QTcpSocket there is a signal readyRead() emited every time that data is written in the socket. Are talking about that? -
Well, there's very much functionality already built in Qt, like waiting on a socket to connect or data to be ready on a peer. In the code above I saw Qt signals and slot mechanism being wrapped around native posix socket code. This is probalbly not that much portabel in comparison to using the Qt-built-in socket functionality, however, coding yourself again may lead to bugs.