C++ (Qt)class CatTcpThread : public QThread{ Q_OBJECTpublic: CatTcpThread(int socketDescription, QObject *parent=0); void run(); QHostAddress GetAddressClient(); private: int m_socketDescriptor; QTcpSocket *m_client; QHostAddress m_addr; public slots: void slot_onRead(); void slot_onDisconnect(); void slot_onGetError(QAbstractSocket::SocketError error); void slot_transmiteDataForClient(QByteArray &); signals: void signal_setup(); void signal_startCat(int); void signal_endWork(); void signal_error(QAbstractSocket::SocketError);}; //******************************************************************************// CatTcpServer//****************************************************************************** class CatTcpServer : public QTcpServer{ Q_OBJECTpublic: CatTcpServer(const QString &iniFileName, QObject *parent = 0); ~CatTcpServer(); bool startServer(); void writeData(QByteArray &block, QString ip); void writeData(QByteArray &block); protected: void removeAllThreads(); void incomingConnection(int socketDescriptor); QList<CatTcpThread*> m_threads; public slots: void slot_removeThread(); void slot_setup(); void slot_startCat(int); void slot_endWork(); signals: void signal_transmiteDataForClient(QByteArray &);private: ... };
C++ (Qt)void CatTcpThread::run(){ QByteArray block; m_client = new QTcpSocket(); QHostAddress local; if(!m_client->setSocketDescriptor(m_socketDescriptor)) { return; } connect(m_client, SIGNAL(readyRead()), this, SLOT(slot_onRead()), Qt::DirectConnection); connect(m_client, SIGNAL(disconnected()), this, SLOT(slot_onDisconnect()), Qt::DirectConnection); connect(m_client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_onGetError(QAbstractSocket::SocketError)), Qt::DirectConnection); m_addr = m_client->peerAddress(); exec();} // Отправить данные клиентуvoid CatTcpThread::slot_transmiteDataForClient(QByteArray &block){ if(block.size() > 0) { if(m_client->write(block) < 0) return; if(!m_client->waitForBytesWritten(3000)) return; }} //******************************************************************************// CatTcpServer//****************************************************************************** void CatTcpServer::writeData(QByteArray &block){// emit signal_transmiteDataForClient(block); for(int i = 0; i < m_threads.count(); i++) { // Ошибка возникает вот тут!!! m_threads[i]->slot_transmiteDataForClient(block); }}
C++ (Qt)void CatTcpServer::incomingConnection(int socketDescriptor){ qDebug() << "Incomming connection!"; CatTcpThread *thread = new CatTcpThread(socketDescriptor, this); connect(thread, SIGNAL(finished()), this, SLOT(slot_removeThread()), Qt::DirectConnection); connect(thread, SIGNAL(signal_setup()), this, SLOT(slot_setup())); connect(thread, SIGNAL(signal_startCat(int)), this, SLOT(slot_startCat(int))); connect(thread, SIGNAL(signal_endWork()), this, SLOT(slot_endWork())); connect(this, SIGNAL(signal_transmiteDataForClient(QByteArray &)), thread, SLOT(slot_transmiteDataForClient(QByteArray&))); m_threads.append(thread); this->moveToThread(thread); <- Вот эту строку добавил thread->start();}
C++ (Qt)m_threads[i]->slot_transmiteDataForClient(block);
C++ (Qt)emit signal_transmiteDataForClient(block);
C++ (Qt)void CatTcpThread::slot_transmiteDataForClient(QByteArray block){ if(block.size() > 0) { if(m_client->write(block) < 0) <- вот тут возникает эта ошибка return; if(!m_client->waitForBytesWritten(3000)) return; }}
C++ (Qt)void CatTcpServer::incomingConnection(int socketDescriptor){ qDebug() << "Incomming connection!"; CatTcpThread *thread = new CatTcpThread(socketDescriptor, this); connect(thread, SIGNAL(finished()), this, SLOT(slot_removeThread()), Qt::DirectConnection); connect(thread, SIGNAL(signal_setup()), this, SLOT(slot_setup())); connect(thread, SIGNAL(signal_startCat(int)), this, SLOT(slot_startCat(int))); connect(thread, SIGNAL(signal_endWork()), this, SLOT(slot_endWork())); connect(this, SIGNAL(signal_transmiteDataForClient(QByteArray)), thread, SLOT(slot_transmiteDataForClient(QByteArray))); m_threads.append(thread); thread->start();} void CatTcpServer::slot_removeThread(){ qDebug() << "Remove thread"; CatTcpThread *thread = qobject_cast<CatTcpThread*>(sender()); m_threads.removeAt(m_threads.indexOf(thread)); thread->deleteLater();}
C++ (Qt)void CatTcpServer::incomingConnection(int socketDescriptor){ // CatTcpThread *thread = new CatTcpThread(socketDescriptor, this); // connect(thread, SIGNAL(finished()), this, SLOT(slot_removeThread()), // Qt::DirectConnection); // connect(thread, SIGNAL(signal_setup()), this, SLOT(slot_setup())); // connect(thread, SIGNAL(signal_startCat(int)), this, SLOT(slot_startCat(int))); // connect(thread, SIGNAL(signal_endWork()), this, SLOT(slot_endWork())); // connect(this, SIGNAL(signal_transmiteDataForClient(QByteArray)), // thread, SLOT(slot_transmiteDataForClient(QByteArray))); // m_threads.append(thread); // thread->start(); QTcpSocket *clientConnection = new QTcpSocket(); clientConnection->setSocketDescriptor(socketDescriptor); qDebug() << "Incomming connection!"; clientConnections.append(clientConnection); connect(clientConnection, SIGNAL(readyRead()), this, SLOT(slot_onRead()), Qt::DirectConnection); connect(clientConnection, SIGNAL(disconnected()), this, SLOT(slot_onDisconnect()));}