Originally Posted by
wysota
I don't know if you are aware of this, but the thread object and its members live not in the thread executed by its run() method but in the thread that created the
QThread subclassed object (hence the socket lives in a different thread than the one operating on it). I don't know if it's important here, but it seems to be that you are not aware of that.
Thanks for the heads-up!
I must confess that at first reading I almost got baffled with your input!
Well... what's wrong with using the socket coming from
{
Q_OBJECT
public:
unsigned int nbrClients;
protected:
void incomingConnection ( int socketDescriptor );
};
class Server : public QTcpServer
{
Q_OBJECT
public:
unsigned int nbrClients;
Server ( QObject *parent = 0 );
protected:
void incomingConnection ( int socketDescriptor );
};
To copy to clipboard, switch view to plain text mode
?
and then passing it to
myUnit *tunit = new myUnit ( socketDescriptor,this );
myUnit *tunit = new myUnit ( socketDescriptor,this );
To copy to clipboard, switch view to plain text mode
I mean... I have no other discernible way of passing the socket to the thread...
ADDENDUM:
I create the socket in
{
Q_OBJECT
public:
}
class myUnit : public QThread
{
Q_OBJECT
public:
QTcpSocket tcpSocket;
}
To copy to clipboard, switch view to plain text mode
and in
myUnit
::myUnit ( int socketDescriptor, Server
*parent
) : QThread ( parent
){
// initialize the unit's socket --> provided by Server::incomingConnection()
if ( !tcpSocket.setSocketDescriptor ( socketDescriptor ) )
{
emit error ( tcpSocket.error() );
return;
}
}
myUnit::myUnit ( int socketDescriptor, Server *parent ) : QThread ( parent )
{
// initialize the unit's socket --> provided by Server::incomingConnection()
if ( !tcpSocket.setSocketDescriptor ( socketDescriptor ) )
{
emit error ( tcpSocket.error() );
return;
}
}
To copy to clipboard, switch view to plain text mode
I set its descriptor coming from Server::incomingConnection() ...
What could possibly be wrong with this?
Thanks,
Pedro.
Bookmarks