C++ QT-TcpServer-Max客户端
我想在我的应用程序中使用C++ QT-TcpServer-Max客户端,c++,qt,tcpserver,C++,Qt,Tcpserver,我想在我的应用程序中使用setMaxPendingConnections。我试过这个代码: MainCLass::MainCLass(QObject *parent) : QObject(parent) { this->server = new QTcpServer(this); server->setMaxPendingConnections(2); QObject::connect(this->server, SIGNAL(newConnection()), t
setMaxPendingConnections
。我试过这个代码:
MainCLass::MainCLass(QObject *parent) : QObject(parent)
{
this->server = new QTcpServer(this);
server->setMaxPendingConnections(2);
QObject::connect(this->server, SIGNAL(newConnection()), this, SLOT(newuser()));
if (!this->server->listen(QHostAddress::Any, 33333)) {
qDebug() << "Error create server!";
}
}
void MainCLass::newuser()
{
qDebug() << "New client!";
QTcpSocket* clientSocket=server->nextPendingConnection();
clientSocket->write("Hello!");
qDebug() << "Max connect?" << this->server->hasPendingConnections();
}
MainCLass::MainCLass(QObject*parent):QObject(parent)
{
此->服务器=新QTcpServer(此);
服务器->设置MaxPendingConnections(2);
连接(这个->服务器,信号(newConnection()),这个,插槽(newuser());
如果(!this->server->listen(QHostAddress::Any,33333)){
qDebug()写“你好!”;
qDebug()具有挂起连接();
}
但它不起作用。我的服务器中有两个以上的客户端可以连接。怎么了?哪里出了错?我使用了Qt5。在Windows 7和Ubuntu 12上试用过。这是挂起的连接数;一旦你通过调用
nextPendingConnection
处理一次,它就不再挂起,挂起的连接计数器就会减少。噢。…那么我如何使用客户端的限制?如果您已经处理了足够多的客户端,请不要调用nextPendingConnection
。