1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
MrRissso [65]
3 years ago
12

PROGRAM DESCRIPTION: In this assignment, you will write two complete C programs that will allow two players to play the game of

network tic-tac-toe. It will include two programs, a server and a client. The server will allow two clients to connect and then will begin the game. The client programs accept input from the player and transmits the command to the server which will execute the commands and send a reply back to the client programs. The client and server programs are to communicate via the Internet (network) using TCP sockets. Your server should be able to handle commands from either client in any order. Your clients should be able to handle responses from the server or the player. (hint: use select) The game is for two players on a 3x3 grid. The player who moves first uses X marks. The second player uses O marks. Each player takes turns placing their mark (XJO) on an empty spot on the grid. The game ends when all spots have a mark or either player has 3 marks in a row. REQUIREMENTS: Your code should be well documented in terms of comments. For example, good comments in general consist of a header (with your name, course section, date, and brief description), comments for each variable, and commented blocks of code. Your server should be named "minor4server.c". without the quotes. Your client should be named "minor4client.c", without the quotes. Your programs will be graded based largely on whether it works correctly on the CSE machines (e.g., cse01, cse02, ..., cse06), so you should make sure that your scripts do not have any runtime errors and runs on a CSE machine. This is an individual programming assignment that must be the sole work of the individual student. Any instance of academic dishonesty will result in a grade of "F" for the course, along with a report filed into the Academic Integrity Database.
Computers and Technology
1 answer:
marissa [1.9K]3 years ago
4 0

Answer:

Explanation:

minor4server.c:

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

int player_count = 0;

pthread_mutex_t mutexcount;

void error(const char *msg)

{

perror(msg);

pthread_exit(NULL);

}

/* Reads an int from a client socket. */

int recv_int(int cli_sockfd)

{

int msg = 0;

int n = read(cli_sockfd, &msg, sizeof(int));

if (n < 0 || n != sizeof(int)) /* Client likely disconnected. */

return -1;

#ifdef DEBUG

printf("[DEBUG] Received int: %d\n", msg);

#endif

return msg;

}

/* Writes a message to a client socket. */

void write_client_msg(int cli_sockfd, char * msg)

{

int n = write(cli_sockfd, msg, strlen(msg));

if (n < 0)

error("ERROR writing msg to client socket");

}

/* Writes an int to a client socket. */

void write_client_int(int cli_sockfd, int msg)

{

int n = write(cli_sockfd, &msg, sizeof(int));

if (n < 0)

error("ERROR writing int to client socket");

}

/* Writes a message to both client sockets. */

void write_clients_msg(int * cli_sockfd, char * msg)

{

write_client_msg(cli_sockfd[0], msg);

write_client_msg(cli_sockfd[1], msg);

}

/* Writes an int to both client sockets. */

void write_clients_int(int * cli_sockfd, int msg)

{

write_client_int(cli_sockfd[0], msg);

write_client_int(cli_sockfd[1], msg);

}

/* Sets up the listener socket. */

int setup_listener(int portno)

{

int sockfd;

struct sockaddr_in serv_addr;

/* Get a socket to listen on */

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0)

error("ERROR opening listener socket.");

 

/* Zero out the memory for the server information */

memset(&serv_addr, 0, sizeof(serv_addr));

 

  /* set up the server info */

serv_addr.sin_family = AF_INET;  

serv_addr.sin_addr.s_addr = INADDR_ANY;  

serv_addr.sin_port = htons(portno);      

/* Bind the server info to the listener socket. */

if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)

error("ERROR binding listener socket.");

#ifdef DEBUG

printf("[DEBUG] Listener set.\n");

#endif

/* Return the socket number. */

return sockfd;

}

/* Sets up the client sockets and client connections. */

void get_clients(int lis_sockfd, int * cli_sockfd)

{

socklen_t clilen;

struct sockaddr_in serv_addr, cli_addr;

 

#ifdef DEBUG

printf("[DEBUG] Listening for clients...\n");

#endif

/* Listen for two clients. */

int num_conn = 0;

while(num_conn < 2)

{

/* Listen for clients. */

  listen(lis_sockfd, 253 - player_count);

 

/* Zero out memory for the client information. */

memset(&cli_addr, 0, sizeof(cli_addr));

clilen = sizeof(cli_addr);

 

  /* Accept the connection from the client. */

cli_sockfd[num_conn] = accept(lis_sockfd, (struct sockaddr *) &cli_addr, &clilen);

 

if (cli_sockfd[num_conn] < 0)

/* Horrible things have happened. */

error("ERROR accepting a connection from a client.");

#ifdef DEBUG

printf("[DEBUG] Accepted connection from client %d\n", num_conn);

#endif

 

/* Send the client it's ID. */

write(cli_sockfd[num_conn], &num_conn, sizeof(int));

 

#ifdef DEBUG

printf("[DEBUG] Sent client %d it's ID.\n", num_conn);

#endif

 

/* Increment the player count. */

pthread_mutex_lock(&mutexcount);

player_count++;

printf("Number of players is now %d.\n", player_count);

pthread_mutex_unlock(&mutexcount);

if (num_conn == 0) {

/* Send "HLD" to first client to let the user know the server is waiting on a second client. */

write_client_msg(cli_sockfd[0],"HLD");

 

#ifdef DEBUG

printf("[DEBUG] Told client 0 to hold.\n");

#endif

}

num_conn++;

}

}

/* Gets a move from a client. */

int get_player_move(int cli_sockfd)

{

#ifdef DEBUG

printf("[DEBUG] Getting player move...\n");

#endif

 

/* Tell player to make a move. */

write_client_msg(cli_sockfd, "TRN");

/* Get players move. */

return recv_int(cli_sockfd);

}

/* Checks that a players move is valid. */

int check_move(char board[][3], int move, int player_id)

{

if ((move == 9) || (board[move/3][move%3] == ' ')) { /* Move is valid. */

 

#ifdef DEBUG

printf("[DEBUG] Player %d's move was valid.\n", player_id);

#endif

return 1;

}

else { /* Move is invalid. */

#ifdef DEBUG

printf("[DEBUG] Player %d's move was invalid.\n", player_id);

#endif

return 0;

}

}

/* Updates the board with a new move. */

void update_board(char board[][3], int move, int player_id)

{

board[move/3][move%3] = player_id ? 'X' : 'O';

 

#ifdef DEBUG

printf("[DEBUG] Board updated.\n");

#endif

}

/* Draws the game board to stdout. */

void draw_board(char board[][3])

{

printf(" %c | %c | %c \n", board[0][0], board[0][1], board[0][2]);

printf("-----------\n");

printf(" %c | %c | %c \n", board[1][0], board[1][1], board[1][2]);

printf("-----------\n");

printf(" %c | %c | %c \n", board[2][0], board[2][1], board[2][2]);

}

/* Sends a board update to both clients. */

void send_update(int * cli_sockfd, int move, int player_id)

{

#ifdef DEBUG

printf("[DEBUG] Sending update...\n");

#endif

 

/* Signal an update */

write_clients_msg(cli_sockfd, "UPD");

/* Send the id of the player that made the move. */

write_clients_int(cli_sockfd, player_id);

 

/* Send the move. */

write_clients_int(cli_sockfd, move);

 

#ifdef DEBUG

printf("[DEBUG] Update sent.\n");

#endif

You might be interested in
Which one of the following features can control left and right indents using markers?
abruzzese [7]
D) The styles pane.

Word normally applies the chosen formatting commands when a style is applied. To control the left or the right indent (distance of a paragraph from the left or right of a margin) in a document using markers, one has to use the styles pane.






4 0
3 years ago
Read 2 more answers
1. Wash all work surfaces with a_______ wrung in hot soapy water.
grandymaker [24]

Answer:

sponge or wash cloth

Explanation:

3 0
3 years ago
Lisa and her husband would like to buy a house soon. She continuously pays bills late. How do her actions affect her and others?
stiks02 [169]
Well actually it affects her credit. Home owners would not want to sell a house to someone who doesn't pay bills on time. It shows he/she is unreliable

3 0
3 years ago
Read 2 more answers
Why would Network Systems employees be employed by the government?
Ghella [55]
Well knowing how the government is with security id go with A. as it makes a lot of sense unlike B and D and if they were looking for software the question would mention it
8 0
3 years ago
Read 2 more answers
When two or more tables share the same number of columns, and when their corresponding columns share the same or compatible doma
ryzh [129]

Answer:

:Union compatible

Explanation:

Two parameters are said to be similar or union compatible if both the parameter have similar attribute.

In DBMS, two table are said to be union compatible if they have same attribute and same attribute domain.

It help to draw the attention toward the structure of the table that shows that all the attribute on which table has been drawn are same in both given table.

8 0
4 years ago
Other questions:
  • What does confidentiality of data refer to?
    6·2 answers
  • When you insert an object in a document, Word always inserts it as a floating object. true or false
    14·1 answer
  • Examples of apps include pop-up windows, validation of webform inpts and images that change when a cursor passes over them
    12·1 answer
  • How do you know what memory to purchase for your computer?
    10·1 answer
  • To close a tab, click the ____ button in the web page thumbnail on the tab switcher.
    9·1 answer
  • (True/False). If the definition of an IS is the medium for recording and storing data, and disseminating information, this would
    12·2 answers
  • What type of link is used to call this file?
    8·1 answer
  • To use the Report Wizard to create a report for a query, select the query in the Navigation Pane, click ____ on the ribbon, and
    13·1 answer
  • When two conductors are accidentally connected it is<br> called a
    6·1 answer
  • Judy forgot where she saved a certain file on her computer. Therefore, she searches for all files with a .jpg file extension. Wh
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!