我在游戏结束时有一个带重启按钮的小游戏,第一次这个按钮工作正常,但第二次服务器只接收来自点击按钮的客户端的clientRdy bool。我正在使用系统套接字。
void Client::restartButtonClicked()
{
pocketsExchange->stop();
pocket.isRestartClicked = 1;
qDebug()<<"restartbuttonclicked";
emit hideRsButton();
clientNetwork->send(&pocket,sizeof(pocket)); //notify server and 2nd client that client clicked restart
memset(&pocket,0,sizeof(pocket));
memset(&r_pocket,0,sizeof(r_pocket));
memset(&pieces,0,sizeof(pieces));
reStartGame();
}
当第二个客户收到isSecondClientRestartClicked
时void Client::secondClickedRestart()
{
pocketsExchange->stop();
emit hideRsButton();
qDebug()<<"second client clicked restart";
memset(&pocket,0,sizeof(pocket));
memset(&r_pocket,0,sizeof(r_pocket));
memset(&pieces,0,sizeof(pieces));
reStartGame();
}
客户端重启功能:
void Client::reStartGame()
{
bool clientReady = 1;
clientNetwork->send(&clientReady,sizeof(bool));
qDebug()<<"clientReadySended";
qDebug()<<"client ready to recieve pieces";
clientNetwork->recieve(&pieces,sizeof(F_and_n_pieces));
qDebug()<<"client received pieces";
setFirstAndNextPiece(pieces.firstPiece,pieces.nextPiece);
pocketsExchange->start(30);
emit startSignal();
}
服务器重启功能:
void Server::reStart()
{
bool c1rdy = 0;
bool c2rdy = 0;
pocketsExchange->stop();
memset(&c1pocket,0,sizeof(c1pocket));
memset(&c2pocket,0,sizeof(c2pocket));
pieces.firstPiece.setRandomShape();
pieces.nextPiece.setRandomShape();
nextPiecesList.push_back(nextPiece);
serverNetwork->recvOne(0,&c1rdy,sizeof(bool));
serverNetwork->recvOne(1,&c2rdy,sizeof(bool));
qDebug()<<"ServerComesHere "<<c1rdy<<" "<<c2rdy; // first time its "true true" but second time true on only client who pressed button, second is "false"
if (c1rdy == 1 && c2rdy == 1)
{
c1rdy = 0;
c2rdy = 0;
serverNetwork->sendAll(&pieces,sizeof(CF_and_n_pieces));
pocketsExchange->start(30);
qDebug()<<"ServerComesHere2nd";
}
}