void chat_deamon::incomingConnection(int socketDescriptor){chat_trhead *thread = new chat_trhead(socketDescriptor, this);connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));connect(thread, SIGNAL(post(QString)), this, SLOT(post(QString)) );connect(this, SIGNAL(users_upd(QString)) , thread, SLOT(upd_users(QString)) );thread->start();}
void chat_trhead::run(){QTcpSocket tcpSocket;if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {emit error(tcpSocket.error());return;}while(1){// читаем и т.д.}
void chat_trhead::upd_users(QString nul){ ul = nul; upd_u = true;}
void chat_trhead::run(){ QTcpSocket tcpSocket; connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(тут какой нить слот)); if (!tcpSocket.setSocketDescriptor(socketDescriptor)) { emit error(tcpSocket.error()); return; } exec();}
class MyThread : public QThread { public: void run(); }; void MyThread::run() { QTcpSocket socket; // connect QTcpSocket's signals somewhere meaningful ... socket.connectToHost(hostName, portNumber); exec(); }
void CTcpCast::StartServer(qint32 nPort){ if (!listen(QHostAddress::Any, nPort)) return;}void CTcpCast::StopServer(){ this->close();}void CTcpCast::incomingConnection(qint32 nSocketDescriptor){ CThreadWorker *m_pThread = new CThreadWorker(nSocketDescriptor, this); connect(m_pThread, SIGNAL(finished()), m_pThread, SLOT(deleteLater())); m_pThread->start();}
CThreadWorker::CThreadWorker(int nSocketDescriptor, QObject *parent): QThread(parent), m_SockedDescriptor(nSocketDescriptor){...}void CThreadWorker::run(){ CConnection m_Connection; if(!m_Connection.setSocketDescriptor(m_SockedDescriptor)) return; connect(&m_Connection, SIGNAL(disconnected()), this, SLOT(deleteConnection())); exec();}
CConnection::CConnection(QObject *parent) : QTcpSocket(parent){ connect(this, SIGNAL(readyRead()), this, SLOT(processReadyRead())); ...}....void CConnection::processReadyRead(){ char *szData; QString cReadedData; QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_0); if(this->isReadable()) { qint64 qnSize = this->bytesAvailable(); if( qnSize > 0) { szData = new char [qnSize+sizeof(szData)]; qint64 qnReadSize = this->read(szData, qnSize); szData[qnReadSize] = NULL; } } cReadedData.append(szData); if(szData != NULL) delete[] szData; qDebug() << cReadedData.simplified(); ....}