使用Qt的多客户端服务器的方法

时间:2012-12-28 16:04:31

标签: c++ qt networking tcp threadpool

我正在使用Qt lib在C ++中创建一个多客户端服务器(IRC)。我想知道它是否是一种很好的服务器架构方法。

我想避免为每个连接创建线程,所以我认为我可以在某种容器中拥有所有客户端套接字并使用ThreadPool执行操作(如处理传入的数据包)。

我唯一关心的是连接到SLOT的套接字是否保证并行客户端处理。

代码:

CServer::CServer(QObject *parent) : QTcpServer(parent)
{
        server = new QTcpServer(this);
        connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));

        if(!server->listen(QHostAddress::Any, 6667)) 
            qDebug() << "Oh noes";
}

void CServer::newConnection(){
           add server->nextPendingConnection() to the container
}

1 个答案:

答案 0 :(得分:0)