Question

In: Computer Science

Complete the code. You are given a zip file that contains an online multiplayer Tic-tac-toe system....

Complete the code. You are given a zip file that contains an online multiplayer Tic-tac-toe system. The system comprises two main modules: Server and Client and uses server – client communication between them. You will have to extend the client and server files to include the missing communications using socket programming. In this game, the server runs indefinitely, waiting for a client. A client connects to the server to see if there are other players online. A client then invites one of the players to play the game. If the asked player accepts the invitation, the game will start. At the end of the game, the two players can either play again or quit the game and return to the server command line. The Tic-tac-toe Client: In the beginning, the client will prompt the player to specify the server IP address. The client will then list all the commands supported by the client. There are four commands included in the client implementation: join, list, invite and leave. The client creates two different threads to handle the connection with the server and the opponent client. Client commands: join: followed by a username that the player wishes to use in the game. list: list the usernames of all online players. invite: send a play request to another player. leave: leave the game; this will remove the player name from the online player list. The Tic-tac-toe Server: The server maintains clients' information in a hash-map data structure where the client IP address and username are used as a key; this allows multiple clients with a different IP address to use the same username. The server uses a multi-threaded approach to concurrent server design: for each client, a new thread is created to handle the client request. This thread will exit when the client sends a leave command to the server.

Fill in the coed that is numbered

SERVER.C

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "uthash.h"/*Create and maintain HashMap functions*/

#define QLEN 15 /* max number of pending incoming connections */
#define BUFFERSIZE 1500 /* max size of buffer*/
#define STRLEN 100 /* max string length */

typedef struct {
char name [STRLEN];
char ip_addr [STRLEN];
} record_key_t;

typedef struct {
record_key_t key;
int status;
UT_hash_handle hh;
} record_t;

/* functions prototype*/

void * server_thd(void * parm);       /* a function to handle communication with clients */

void get_online_Players(const void *key, const void *value, void *obj); /* a function to handle the li$

int add_user(char name [STRLEN], char ip_addr [STRLEN]);

void print_users();

void user_left(char name [STRLEN], char ip_addr [STRLEN]);

void user_join(record_t *p);

record_t *users = NULL; /* init. hashmap list*/

pthread_mutex_t mut; /* Mutex to prevent multiple write. */

int size = 0;

int main () /* the main function*/

{

    struct   sockaddr_in sarver_ad;      /* to store server local ip and port */

    struct   sockaddr_in client_ad;      /* to store client ip and port */

    int      server_fd, client_fd;                    /* socket descriptors */

    int      port = 9090;                /* protocol port number */

    int      addr_len;                       /* length of address */

    pthread_t thrd_id;                      /* variable to hold thread ID */

    pthread_mutex_init(&mut, NULL);

    bzero( &sarver_ad, sizeof(sarver_ad));      /* clear sockaddr struct.*/

    /* 1) here you need to set up the sockaddr_in*/

    /* set address family */

   

    /* set the local IP address to any local address */

    /* set the local port to 9090 */

    /* Create a socket */

    /* 2) here you need to create a socket*/

    /* 3) here you need to create a socket Bind a local address to the socket */

    /* 4) here you need to make the server listen on server_fd for incoming connections */

    /* Main server loop - accept and handle requests */

    fprintf(stderr, "Server up and running.\n");

    while (1) {

        /* 5) here you need to make accept incoming connections before the new thread is created*/

        /* Create seperate thread for each client */

        pthread_create(&thrd_id, NULL, server_thd, (void *) client_fd );

    }

close(server_fd); /* Close socket */

}

int add_user(char name [STRLEN], char ip_addr [STRLEN])

CLIENT.C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <stdbool.h>
#include <pthread.h>

#define MAXBUFFER 100 /*Maximum buffer size*/
#define MAXSTRINGLEN 100 /*Maximum String length*/

#define SERVERPORTNUM 9090 /*server port number*/

#define HOSTPORTNUM 12345 /* Local port number */
#define PEERPORTNUM 12345 /* peer port number */

char name[MAXSTRINGLEN], /* name used to join the server */
peer_name[MAXSTRINGLEN];/* name of opponent */
pthread_t thrid_id; /*thread Id*/

int current_player=0; /* local user score*/
int other_player=0; /* remote user score*/


void tic_tac_toe(int socket, char * buffer, int playerID); /*the tic tac toe game.*/
void * serverConn(void * parm); /* Thread function to handle communication with the server */
void * peerConn(char * ip); /* Thread function used when we want to open a connection with$
void print_instraction(); /* to print the game instructions */

int server_socket, peer_socket, listn_socket, consocket; /* All socket descriptors are stored as globa$
int ingame, turn; /* Some variables to handle the flow of the game. */

int main(int argc, char *argv[])
{
printf("Enter Server's IP: ");
const char server[30]; /* store IP address of server */
scanf("%s",server);

/* Establish connection with the server */
struct sockaddr_in dest;
/* 1) here you need to create a socket named 'server_socket' to connect to the server */

memset(&dest, 0, sizeof(dest));
/* 2) here you need to creat sockaddr_in to store server info.*/


/*connect to the server*/
/* 3) here you need to 'server_socket' to the server.*/

pthread_create(&thrid_id, NULL, serverConn, (void*) &server_socket);

/* For Invitation */
/* Bind to a socket and start listening for incomming connections. peer-to-peer*/
struct sockaddr_in peer;
struct sockaddr_in serv;
socklen_t socksize = sizeof(struct sockaddr_in);
memset(&serv, 0, sizeof(serv));
/* 4) here you need to set up the sockaddr_in*/
/* 5) here you need to create a socket*/
/* 6) here you need to create a socket Bind a local address to the socket */
/* 7) here you need to make the server listen on server_fd for incoming connections, Only accept o$
/* 8) here you need to make accept incoming connections before the new thread is created*/

char buffer[MAXBUFFER]; /* Buffer to hold data exchanged */
int len;

/* handle peer connection request */
while(consocket)
{
pthread_cancel(thrid_id);
/* Receive peer's name and store it as current opponent */
len = recv(consocket, buffer, MAXBUFFER, 0);
buffer[len] = '\0';
strcpy(&peer_name, buffer);
printf("\n%s has challenged you. Accept challenge? (y/n) ", peer_name);

/* Ask for approval */
fgets(buffer, sizeof(buffer), stdin);
buffer[strlen(buffer)-1] = '\0';
if (strcmp(buffer, "y") == 0)
{
current_player=0;
other_player=0;
printf("\nYour Score: %d", current_player);
printf("\nOpponent's Score: %d",other_player);
ingame = 1;
send(consocket, buffer, strlen(buffer), 0);
}
else

{
current_player=0;
other_player=0;
send(consocket, buffer, strlen(buffer), 0);
printf("\nYou declined...\n");
ingame = 0;
}

/* Play game until one of the player declines. */
turn = 0;
int playerID = 2;

/* Close all sockets before the program exits */
close(listn_socket);
close(consocket);
close(peer_socket);
close(server_socket);

return EXIT_SUCCESS;
}


  

Solutions

Expert Solution

Solution: Add following lines in the mentioned position. I have solved few parts, kindly try to solve remaining on your own.

In SERVER.C

  • (1.) here you need to set up the sockaddr_in
​​​​​​​sarver_ad.sin_family = AF_INET;
sarver_ad.sin_addr.s_addr = INADDR_ANY;
sarver_ad.sin_port = htons(port);

   

  • (2.) here you need to create a socket

​​​​​​​if((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
   error("Error thrown by Socket");
}
  • (3.) here you need to create a socket Bind a local address to the socket

​if(bind(server_fd, (struct sockaddr *) &sarver_ad, sizeof(sarver_ad)) == -1) {
    error("Error thrown by Bind");
}​

  

  • (4.) here you need to make the server listen on server_fd for incoming connections

if(listen(server_fd, 10) < 0){
   error("Error thrown by Listen");
}

   

  • (5.) here you need to make accept incoming connections before the new thread is created

socklen_t client_len = sizeof(client_ad);

client_fd = accept(server_fd, (struct sockaddr *) &client_ad, &client_len);
if(client_fd < 0){
   error("Error thrown by Accept");
}

In CLIENT.C

  • (1.) here you need to create a socket named 'server_socket' to connect to the server

if((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0){
        error("Error thrown by Socket");
}
  • (2.) here you need to creat sockaddr_in to store server info.*/

struct hostent *server_entry;                             // Fetches ipv4 address
server_entry = gethostbyname(server);

dest.sin_family = AF_INET;
bcopy((char*) server_entry->h_addr, (char *) &dest.sin_addr.s_addr, server_entry->h_length);
dest.sin_port = htons(SERVERPORTNUM);


  • (3.) here you need to connect 'server_socket' to the server.

if(connect(server_socket, (struct sockaddr *) &dest, sizeof(dest)) < 0){
        error("Error thrown by Connect");
}

Similarly solve the remaining parts.

Let me know if you face doubt with any part.


Related Solutions

Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human players who alternate entering their moves on the same computer. Create a 3-by-3 two-dimensional array. Each player indicates their moves by entering a pair of numbers representing the row and column indices of the square in which they want to place their mark, either an 'X' or an 'O'. When the first player moves, place an 'X' in the specified square. When the second...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has...
C# (Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play...
C# (Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array of integers. The constructor should initialize the empty board to all 0s. Allow two human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game...
If anyone can please write a code for a 5x5 tic tac toe game in matlab...
If anyone can please write a code for a 5x5 tic tac toe game in matlab I would greatly appreciate it. Its extra credit. PLEASE HELP ME :(
Provided is code for checking horizontal and vertical win in a tic tac toe game. Please...
Provided is code for checking horizontal and vertical win in a tic tac toe game. Please provide code for checking diagonal win. function checkWinVertical(player, row, col) { var counter = 0; //each button var btn = document.getElementsByTagName("button"); for (counterC = 0; counterC < col; counterC++){ for (countR = 0; countR < row; countR++){ if (player != btn[counter].innerHTML){ counter += 1; break; } else if (countR + 1 == col) { for (countW = 0; countW < col; countW++){ btn[counter -...
PLEASE READ VERY CAREFULLY write a client.py and server.py file for tic-tac-toe IN PYTHON with the...
PLEASE READ VERY CAREFULLY write a client.py and server.py file for tic-tac-toe IN PYTHON with the following restrictions (SO WRITE TWO FILES THAT PLAY PYTHON THROUGH A SOCKET) Use a 5 x 5 grid (dimensions are subject to change, so use constants for NUM_ROWS and NUM_COLS) Use 'X' for player 1 and 'O' for player 2 (symbols and the number of players is subject to change, so use constants) Each player can make 1 move per turn before having to...
PLEASE READ CAREFULLY!!!! write a client.py and server.py file for tic-tac-toe IN PYTHON with the following...
PLEASE READ CAREFULLY!!!! write a client.py and server.py file for tic-tac-toe IN PYTHON with the following restrictions (SO WRITE TWO FILES THAT PLAY PYTHON THROUGH A SOCKET) Use a 5 x 5 grid (dimensions are subject to change, so use constants for NUM_ROWS and NUM_COLS) Use 'X' for player 1 and 'O' for player 2 (symbols and the number of players is subject to change, so use constants) Each player can make 1 move per turn before having to wait...
Game of Tic Tac Toe with the following conditions A point system where a  move that leads...
Game of Tic Tac Toe with the following conditions A point system where a  move that leads to a winning game is given 1 point, a move that leads to a tie is given 0 point, and a  lead to a losing game will get -1 point. Grid size of 5x5 A timer that can be set for how long a game can be played 5 symbols in a row to get a point Connected lines cannot be crossed (No diagonal lines)...
In Python: Please complete the following two tasks in Tic Tac Toe: 1. Allow the user...
In Python: Please complete the following two tasks in Tic Tac Toe: 1. Allow the user to choose heads/tails to see if the user goes first or the computer and alter your code accordingly. 2. Allow the user to play again. Add a simple strategy for the AI to play against the user.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT