struct conn { QTcpSocket* connection; QScsConnectionHandler* handler;}; QTcpServer scs_server; QVector<conn> connArray; conn connCurrent;
forever { QTcpServer scs_server; scs_server.listen(QHostAddress::Any, 7001); scs_server.waitForNewConnection(500, &timedOut); while(scs_server.hasPendingConnections()) { connCurrent.connection = scs_server.nextPendingConnection(); connCurrent.handler = new QScsConnectionHandler(connCurrent.connection); connArray.append(connCurrent); connCurrent.handler->start(); } for (int i0=0;i0<connArray.size();i0++) { if(!connArray.at(i0).handler->new_connection) { delete connArray.at(i0).handler; connArray.remove(i0); } } if(checkForExit()) break; }
class QScsConnectionHandler: public QThread{public: QScsConnectionHandler(QTcpSocket* n_conn) { new_connection = n_conn; } ~QScsConnectionHandler() { } virtual void run(); QTcpSocket* new_connection;protected:private: QString tmpStr, headerStr, currentID, currentIDn; QByteArray b0a;};void QScsConnectionHandler::run(){ forever { // process incoming connection std::cout << "start\n"; tmpStr = "200 OK\r\n"; b0a.clear(); b0a.append(tmpStr); new_connection->write(b0a); new_connection->flush(); std::cout << "stop\n"; new_connection->close(); break; } new_connection = NULL;}
class SmSrv : public QTcpServer{ Q_OBJECTpublic: SmSrv(QObject *parent = 0); ~SmSrv(); QVector<SmConnHandler*> cns; void checkForInactiveConnections();private:protected: void incomingConnection(int handle);};
#include "smsrv.h"SmSrv::SmSrv(QObject *parent) : QTcpServer(parent){ listen(QHostAddress::Any, 7001);}SmSrv::~SmSrv(){}void SmSrv::checkForInactiveConnections(){ for (int i0=0;i0<cns.size();i0++) if(cns.at(i0)->isFinished()) { delete cns.at(i0); cns.remove(i0); }}void SmSrv::incomingConnection(int handle){ SmConnHandler* cn = new SmConnHandler(handle, this); cns.append(cn); cn->start();}
struct conn {
QTcpServer scs_server; QVector<QScsConnectionHandler *> connArray;
QTcpServer scs_server;scs_server.listen(QHostAddress::Any, 7001);forever scs_server.waitForNewConnection(500, &timedOut); while(scs_server.hasPendingConnections()) { conHandler * connCurrent = new QScsConnectionHandler( scs_server.nextPendingConnection() ); connHandler -> start(); connArray.append( conHandler); } if(checkForExit()) break;}
class QScsConnectionHandler: public QThread{public: QScsConnectionHandler(QTcpSocket* n_conn) { new_connection = n_conn; } ~QScsConnectionHandler() { } virtual void run(); QTcpSocket* new_connection;protected:private: QString tmpStr, headerStr, currentID, currentIDn; QByteArray b0a;};void QScsConnectionHandler::run(){ while ( new_connection -> state!= QAbstractSocket::UnconnectedState ) { // process incoming connection только тогда здесь перед close желательно использовать сначала disconnect } deleteLater ();}
#include <QtCore/QCoreApplication>#include <QDatetime>#include <QStringList>#include <QVariant>#include <iostream>#include "qscsmisc.h"#include "smsrv.h"int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); SmSrv srv; while(srv.isListening()) { a.processEvents(); srv.checkForInactiveConnections(); if(checkForExit()) break; } return 0; /*a.exec();*/}
#ifndef SMSRV_H#define SMSRV_H#include <QObject>#include <QtNetwork>#include <QtSql>#include <QThread>#include <QTcpSocket>#include "smconnhandler.h"class SmSrv : public QTcpServer{ Q_OBJECTpublic: SmSrv(QObject *parent = 0); ~SmSrv(); QVector<SmConnHandler*> cns; void checkForInactiveConnections();private:protected: void incomingConnection(int handle);};#endif // SMSRV_H
#include "smsrv.h"SmSrv::SmSrv(QObject *parent) : QTcpServer(parent){ listen(QHostAddress::Any, 7001);}SmSrv::~SmSrv(){}void SmSrv::checkForInactiveConnections(){ for (int i0=0;i0<cns.size();i0++) if(cns.at(i0)->isFinished()) { delete cns.at(i0); cns.remove(i0); }}void SmSrv::incomingConnection(int handle){ std::cout << "+1\n"; SmConnHandler* cn = new SmConnHandler(handle, this); cns.append(cn); cn->start();}
#ifndef SMCONNHANDLER_H#define SMCONNHANDLER_H#include <QThread>#include <QTcpSocket>#include <iostream>#include <stdlib.h>class SmConnHandler : public QThread{ Q_OBJECTpublic: SmConnHandler(int socketDescriptor, QObject *parent); ~SmConnHandler(); void run(); QTcpSocket* new_connection; int sDescr;private: };#endif // SMCONNHANDLER_H
#include "smconnhandler.h"SmConnHandler::SmConnHandler(int socketDescriptor, QObject *parent) : QThread(parent){ sDescr = socketDescriptor;}SmConnHandler::~SmConnHandler(){}void SmConnHandler::run(){ QByteArray b0a; QString tmpStr, headerStr, currentID, currentIDn; //new_connection = new QTcpSocket; -- хотя и работает, но выдает ошибку, см. ниже. QTcpSocket new_connection; new_connection.setSocketDescriptor(sDescr); forever { std::cout << "start\n"; tmpStr = "200 OK\r\n"; b0a.clear(); b0a.append(tmpStr); //new_connection->write(b0a); //если обратиться к write() по указателю, будет ошибка //QObject: Cannot create children for a parent that is in a different thread. (Parent is //QNativeSocketEngine(0086C4A0), parent's thread is QThread(00865828), current thread is //SmConnHandler(0086B990) new_connection.write(b0a); new_connection.flush(); std::cout << "stop\n"; new_connection.close(); break; }}
QTcpSocket * new_connection = new QTcpSocket(this);
QTcpSocket new_connection(this);
QTcpSocket * new_connection = new QTcpSocket;
QTcpSocket new_connection; // без (this);
QTcpSocket::QTcpSocket(QObject *parent) : QAbstractSocket(TcpSocket, *new QTcpSocketPrivate, parent){#if defined(QTCPSOCKET_DEBUG) qDebug("QTcpSocket::QTcpSocket()");#endif d_func()->isBuffered = true;}QAbstractSocket::QAbstractSocket(SocketType socketType, QAbstractSocketPrivate &dd, QObject *parent) : QIODevice(dd, parent){ Q_D(QAbstractSocket);#if defined(QABSTRACTSOCKET_DEBUG) qDebug("QAbstractSocket::QAbstractSocket(%sSocket, QAbstractSocketPrivate == %p, parent == %p)", socketType == TcpSocket ? "Tcp" : socketType == UdpSocket ? "Udp" : "Unknown", &dd, parent);#endif d->socketType = socketType;}QIODevice::QIODevice(QIODevicePrivate &dd, QObject *parent) : QObject(dd, parent){}QObject::QObject(QObjectPrivate &dd, QObject *parent) : d_ptr(&dd){ Q_D(QObject); ::qt_addObject(d_ptr->q_ptr = this); d->threadData = QThreadData::current(); d->threadData->ref(); if (!check_parent_thread(parent, parent ? parent->d_func()->threadData : 0, d->threadData)) parent = 0; if (d->isWidget) { if (parent) { d->parent = parent; d->parent->d_func()->children.append(this); } // no events sent here, this is done at the end of the QWidget constructor } else { setParent(parent); }}static bool check_parent_thread(QObject *parent, QThreadData *parentThreadData, QThreadData *currentThreadData){ if (parent && parentThreadData != currentThreadData) { QThread *parentThread = parentThreadData->thread; QThread *currentThread = currentThreadData->thread; qWarning("QObject: Cannot create children for a parent that is in a different thread.\n" "(Parent is %s(%p), parent's thread is %s(%p), current thread is %s(%p)", parent->metaObject()->className(), parent, parentThread ? parentThread->metaObject()->className() : "QThread", parentThread, currentThread ? currentThread->metaObject()->className() : "QThread", currentThread); return false; } return true;}