From 4e5170caf82f437fede34b0f7a4513af5099ff1a Mon Sep 17 00:00:00 2001 From: CalciteM Team Date: Sun, 28 Jul 2019 19:45:49 +0800 Subject: [PATCH] =?UTF-8?q?network:=20=E8=83=BD=E8=87=AA=E5=8A=A8=E5=9C=A8?= =?UTF-8?q?30001/30002=E7=AB=AF=E5=8F=A3=E4=B9=8B=E9=97=B4=E9=80=89?= =?UTF-8?q?=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NineChess/src/aithread.cpp | 12 ++++++++---- NineChess/src/gamecontroller.cpp | 9 +++------ NineChess/src/server.cpp | 19 +++++++++++++------ 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/NineChess/src/aithread.cpp b/NineChess/src/aithread.cpp index 1dae37aa..1e2cedaa 100644 --- a/NineChess/src/aithread.cpp +++ b/NineChess/src/aithread.cpp @@ -41,15 +41,19 @@ AiThread::AiThread(int id, QObject *parent) : connect(&timer, &QTimer::timeout, this, &AiThread::act, Qt::QueuedConnection); // 网络 - server = new Server(nullptr, 30000 + id); - client = new Client(nullptr, id == 1? 30002 : 30001); + if (id == 1) + { + server = new Server(nullptr, 30001); + uint16_t clientPort = server->getPort() == 30001 ? 30002 : 30001; + client = new Client(nullptr, clientPort); + } } AiThread::~AiThread() { // 网络相关 - delete server; - delete client; + //delete server; + //delete client; stop(); quit(); diff --git a/NineChess/src/gamecontroller.cpp b/NineChess/src/gamecontroller.cpp index f731941e..e0a13cc8 100644 --- a/NineChess/src/gamecontroller.cpp +++ b/NineChess/src/gamecontroller.cpp @@ -902,10 +902,10 @@ bool GameController::command(const QString &cmd, bool update /* = true */) if (isEngine1) { ai1.getServer()->setAction(cmd); - qDebug() << "AI(1) set Action: " << cmd; + qDebug() << "isEngine1: AI(1) set Action: " << cmd; } else if (isEngine2) { - ai2.getServer()->setAction(cmd); - qDebug() << "AI(2) set Action: " << cmd; + ai1.getServer()->setAction(cmd); // 注意: 同样是AI1 + qDebug() << "isEngine2: AI(1) set Action: " << cmd; } return true; @@ -1080,7 +1080,4 @@ void GameController::showNetworkWindow() { ai1.getServer()->show(); ai1.getClient()->show(); - - ai2.getServer()->show(); - ai2.getClient()->show(); } diff --git a/NineChess/src/server.cpp b/NineChess/src/server.cpp index 93c0b370..37f3cdbc 100644 --- a/NineChess/src/server.cpp +++ b/NineChess/src/server.cpp @@ -111,11 +111,19 @@ void Server::sessionOpened() tcpServer = new QTcpServer(this); if (!tcpServer->listen(QHostAddress::LocalHost, port)) { - QMessageBox::critical(this, tr("Server"), - tr("Unable to start the server: %1.") - .arg(tcpServer->errorString())); - close(); - return; + port++; + if (!tcpServer->listen(QHostAddress::LocalHost, port)) { + QMessageBox::critical(this, tr("Server"), + tr("Unable to start the server: %1.") + .arg(tcpServer->errorString())); + close(); + return; + } else { + QMessageBox::information(this, tr("Server"), tr("server Started %1.").arg(port)); + } + } + else { + QMessageBox::information(this, tr("Server"), tr("server Started %1.").arg(port)); } QString ipAddress; @@ -159,4 +167,3 @@ void Server::sendAction() clientConnection->write(block); clientConnection->disconnectFromHost(); } -