void MainWindow::connectToNetwork() { // Se invoca cuando se presiona el boton Connect. Solicitar los datos de conexion al // usuario usando un dialogo para tal fin ConnectDialog* dialog = new ConnectDialog(this); // Muestra al dialogo en forma modal con el metodo exec(). Es decir, el dialogo reacciona // a eventos, mientras que el mainWindow se mantiene bloqueado. Esto ocurre por invocar al // metodo exec(), el cual impone otro ciclo de eventos que oculta el ciclo de eventos de la // aplicacion. Si se quiere que tanto el dialog como el MainWindow reaccionen a eventos // se invocaria al metodo dialog->show() if ( dialog->exec() ) { // El dialogo fue aceptado. Obtener los datos escritos del usuario y establecer la conexion quint16 port = dialog->getPort().toUInt(); // El establecimiento de la conexion depende de si el usuario marco "Servidor" o "Cliente" if ( dialog->shouldConnectAsServer() ) connectAsServer(port); else connectAsClient(dialog->getHost(), port); } }
//Begins the connection void establishConnection(char *type, int *replay, char *ip) { //Ensure players cannot leave signal(SIGINT, leaveError); //Server variables struct sockaddr_in dataAddr; int clientSocket; struct sockaddr_in myAddr; int i, addrSize; char buffer[50]; //Game variables char o_phrase[40]; char my_phrase[40]; char h_o_phrase[40]; char h_my_phrase[40]; char board[1000]; board[0] = '\0'; char guess[80]; guess[0] = '\0'; int bytesRcv; int win = 0; //Server connect if (strcmp(type, "server") == 0) { setupSocket(&myListenSocket, &myAddr, type, ""); bindSocket(&myListenSocket, &myAddr); waitForConnection(&myListenSocket, &myAddr, &clientSocket, &dataAddr); //fix } //Client connect if (strcmp(type, "client") == 0) { setupSocket(&clientSocket, &dataAddr, type, ip); connectAsClient(&clientSocket, &dataAddr); } //Loop while the game is supposed to be played again while (*replay == 1) { //Creates the player's phrase, hides it and stores the unhidden phrase in the buffer game_createPhrase(my_phrase); game_toHidden(my_phrase, h_my_phrase); strcpy(buffer, my_phrase); //Send the phrase to the other player and wait for them to send back send(clientSocket, buffer, strlen(buffer), 0); fprintf(stdout, "Waiting for your opponent to create their secret phrase...\n"); //Store what was sent back in the opponent's phrase variable bytesRcv = recv(clientSocket, buffer, sizeof(buffer), 0); buffer[bytesRcv] = '\0'; strcpy(o_phrase, buffer); //Hide their phrase and create the board to print on screen game_toHidden(o_phrase, h_o_phrase); game_makeBoard(h_my_phrase, h_o_phrase, board); //Show the player their own phrase fprintf(stdout, "Your phrase: %s\n\n", my_phrase); //Counter to make the client go first initially i = 0; //Game loop while (1) { //Ensures that the client goes first if (i > 0 || strcmp(type, "server") == 0) { //Wait for opponent to guess and store guess in buffer fprintf(stdout, "Waiting for your opponent's guess...\n"); bytesRcv = recv(clientSocket, buffer, sizeof(buffer), 0); buffer[bytesRcv] = '\0'; strcpy(guess, buffer); //Compare guess with the actual phrase and check for win condition (1 = win, 0 = no win) game_guess(guess, my_phrase, h_my_phrase, &win); if (win == 1) { win = 0; //Wait to see if the opponent wants to play again fprintf(stdout, "Your opponent has won. Please wait while he decides he wants to play again...\n"); bytesRcv = recv(clientSocket, buffer, sizeof(buffer), 0); buffer[bytesRcv] = '\0'; //If opponent sends back "left", we need to do some things depending on if the opponent was the host or not if (strcmp(buffer, "left") == 0) { fprintf(stdout, "Your opponent left!\n"); //If you were the server and lost if (strcmp(type, "server") == 0) { //Re-setup the socket and listen for a new connection setupSocket(&clientSocket, &myAddr, type, ""); waitForConnection(&myListenSocket, &myAddr, &clientSocket, &dataAddr); i = 0; break; } //Close the client socket, change player type to server and listen for a new connection else { close(clientSocket); strcpy(type, "server"); setupSocket(&myListenSocket, &dataAddr, type, ""); waitForConnection(&clientSocket, &dataAddr, &myListenSocket, &myAddr); //TODO fix break; } *replay = -1; return; } //If they didn't send back "left", then they want to play again //So simply break out of the game loop to start a new game with new phrases else { system("clear"); fprintf(stdout, "Your opponent wants to play again, let's go!\n"); break; } } //Update the game board game_makeBoard(h_my_phrase, h_o_phrase, board); fprintf(stdout, "Your phrase: %s\n\n", my_phrase); fprintf(stdout, "Your opponent guessed: %s\n", guess); } //Ask for user input fprintf(stdout, "Guess a letter or the entire phrase.\n"); fgets(guess, sizeof(guess), stdin); guess[strlen(guess)-1] = '\0'; //Compare guess with the actual phrase and check for win condition (1 = win, 0 = no win), then update the game baord game_guess(guess, o_phrase, h_o_phrase, &win); game_makeBoard(h_my_phrase, h_o_phrase, board); fprintf(stdout, "Your phrase: %s\n\n", my_phrase); //Send the opponent what the user guessed strcpy(buffer, guess); send(clientSocket, buffer, strlen(buffer), 0); //If the user won, then see if they want to play again if (win == 1) { game_playAgain(replay); //Wants to play again if (*replay == 1) { strcpy(buffer, "stay"); send(clientSocket, buffer, strlen(buffer), 0); win = 0; break; } //Doesn't want to play again else { strcpy(buffer, "left"); send(clientSocket, buffer, strlen(buffer), 0); //Reset SIGINT to its default functionality signal(SIGINT, SIG_DFL); return; } } //Increment i allowing for the server to have a guess i++; } } //Close the final listen socket (never occurs) close(myListenSocket); }