QT TcpServer Max客户端

时间:2016-03-10 20:06:48

标签: c++ qt tcpserver

我想在我的应用程序中使用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();

}

但它不起作用。超过2个客户端可以在我的服务器中连接。怎么了?哪里出错?我用的是Qt5。试过Windows 7和Ubuntu 12

0 个答案:

没有答案