struct Input{ bool startflag; int player; bool t_dead; int t_dir; int t_posx; int t_posy; int t_movex; int t_movey; int b_posx; int b_posy; int b_dir;};
struct PlayerInfo{ int players; int myPlayerNum;};const int startPos[6][2]={{400,400},{0,0}, {400,200} , {200,150}, {600,350}, {300,300} };Widget::Widget(QWidget *parent) : QWidget(parent){ setupUi(this); server = new QTcpServer(this); socket1 = new QTcpSocket(this); socket2 = new QTcpSocket(this); connect(server, SIGNAL(newConnection()), this , SLOT(OnClientConnected())); server->setMaxPendingConnections(2); players=0; server->listen(QHostAddress::Any); label->setText(MyIp::GetMyIp()+":"+QString::number(server->serverPort())); player1 = new Input; player2 = new Input; player1->t_posx = 400; player1->t_posy = 400; player1->t_dir = 1; player1->t_dead = false; player2->t_posx = 0; player2->t_posy = 0; player2->t_dir = 1; player2->t_dead = false;}void Widget::OnClientConnected(){ players++; QMessageBox::information(this,"connection!","client connected!"); switch(players) { case 1: socket1 = server->nextPendingConnection(); connect(socket1, SIGNAL(readyRead()), this, SLOT(OnReceiveData())); connect(socket1, SIGNAL(disconnected()), this, SLOT(OnClientDisconnected())); this->listWidget->addItem(socket1->peerAddress().toString()+":"+QString::number(socket1->peerPort()) ); break; case 2: socket2 = server->nextPendingConnection(); connect(socket2, SIGNAL(readyRead()), this, SLOT(OnReceiveData())); connect(socket2, SIGNAL(disconnected()), this, SLOT(OnClientDisconnected())); this->listWidget->addItem(socket2->peerAddress().toString()+":"+QString::number(socket2->peerPort()) ); break; default:// server->nextPendingConnection()->abort(); players--; QMessageBox::information(this,"disconection!","client disconected because already 2 players!"); }}void Widget::OnClientDisconnected(){ if (socket1->state()!=QAbstractSocket::ConnectedState) this->listWidget->takeItem(0); if(socket2->state()!=QAbstractSocket::ConnectedState) this->listWidget->takeItem(1); players--;}void Widget::OnReceiveData(){ while(socket1->bytesAvailable()) socket1->read((char*)player1,sizeof(*player1)); while(socket2->bytesAvailable()) socket2->read((char*)player2,sizeof(*player2)); SendData();}void Widget::SendData(){ socket1->write((char*)player2,sizeof(*player2)); socket2->write((char*)player1,sizeof(*player1)); player1->b_posx = -1; player1->b_posy = -1; player2->b_posx = -1; player2->b_posy = -1;}void Widget::SendPlayerInfo(){ PlayerInfo *info= new PlayerInfo; info->players=players; info->myPlayerNum=1; socket1->write((char*)info, sizeof(*info)); info->myPlayerNum=2; socket2->write((char*)info, sizeof(*info));}void Widget::on_pushButton_clicked(){ if(players!=2) QMessageBox::information(this,"Fail","Can't start the game with 1 player!"); else SendPlayerInfo();}
C++ (Qt)operator<<(QdataStream &out, const Input &input){out << input.startflag << input.player << input.t_dead >>...;return out;};operator>>(QdataStream &in, Input &input){in >> input.startflag >> input.player >> input.t_dead >>...;return in;};
C++ (Qt)Input input;QdataStream out(socket);out << input;
C++ (Qt)Input *input= new Input;QdataStream out(socket);out << input;