Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h> // use booleans in C
- #include <stdlib.h> // use common functions
- #include <stdio.h> // use input/output functions
- #include <string.h> // use string functions
- #include <unistd.h> // use POSIX functions
- #include <sys/socket.h> // use socket function
- #include <netinet/in.h> // use internet address datatypes
- #include <arpa/inet.h> // use inet_ntoa function
- #include <netdb.h> // use network database functions
- /** Print field represented by given character array.
- The array must contains 9 chars ('O', 'X' or ' ')
- ordered row-wise from top left to bottom right. */
- void printField(char* field)
- {
- printf("+---+---+---+\n");
- for (int row = 0; row < 3; row++) {
- for (int col = 0; col < 3; col++) {
- printf("| %c ", field[row*3+col]);
- }
- printf("|\n+---+---+---+\n");
- }
- }
- /** Simple client application for Tic-Tac-Toe game using UDP.
- Hostname and port number of server are passed as arguments.
- By default, the server is assumed at localhost, port 5001.
- */
- int main(int argc, char* argv[])
- {
- // Read parameters
- if (argc != 3) {
- printf("Usage: TicTacToeClient [<hostname>] [<port>]\n\n");
- }
- char* hostname = argc > 1 ? argv[1] : "localhost";
- int port = argc > 2 ? atoi(argv[2]) : 5001;
- /** -- Setup connection -- */
- //Set server port
- struct hostent* serverInfo = gethostbyname(hostname);
- if (serverInfo == NULL) {
- printf("Failed to get info for host '%s'!\n", hostname);
- return -1;
- }
- // Create UDP/IP socket
- int sock = socket(AF_INET, SOCK_DGRAM, 0);
- if (sock < 0) {
- printf("Failed to create socket!\n");
- return -1;
- }
- // Set address and port of server
- struct sockaddr_in serverAddr;
- serverAddr.sin_family = serverInfo->h_addrtype;
- serverAddr.sin_port = htons(port);
- memcpy(&serverAddr.sin_addr.s_addr, serverInfo->h_addr, serverInfo->h_length);
- unsigned int addrLen = sizeof(serverAddr);
- printf("Expecting server at %s:%d\n\n",
- inet_ntoa(serverAddr.sin_addr), ntohs(serverAddr.sin_port));
- /** -- Main game -- */
- bool running = true;
- char message[16];
- char command[6];
- char field[9];
- memset(field, ' ', 9);
- printField(field);
- while (running)
- {
- // Read row and column from user
- int row = 0, col = 0;
- printf("Select row and column (1-3): ");
- scanf("%d %d", &row, &col);
- while (getchar() != '\n') {} // discard remaining input
- if (row < 1 || row > 3 || col < 1 || col > 3) {
- printf("Invalid row or column!\n");
- continue;
- }
- // Update field
- int idx = (row-1)*3 + col-1;
- if (field[idx] != ' ') {
- printf("This field is already occupied!\n");
- continue;
- }
- field[idx] = 'X';
- printField(field);
- // TODO : Send message to server, receive and process response
- memset(message, 0, 16);
- strncpy(command, message, 6);
- strncpy(field, message+6, 9);
- message[16] = '\0';
- // Send message to server
- if (sendto(sock, message, 16, 0, (struct sockaddr*)&serverAddr, addrLen) < 0) {
- printf("Failed to send message to server!\n");
- running = false;
- continue;
- } else {
- printf("Sent message to server: \"%s\"\n", message);
- }
- // Receive response from server
- printf("Waiting for response...\n");
- memset(message, 0, 16);
- if (recvfrom(sock, message, 1024, 0, (struct sockaddr*)&serverAddr, &addrLen) < 0) {
- printf("Failed to receive response from server!\n");
- running = false;
- continue;
- }
- memcpy(command, message, 5);
- command[5] = '\0';
- memcpy(field, message + 6, 9);
- // Check message and field and respond
- if (strcmp(command, "FIELD") == 0) {
- printField(field);
- continue;
- }
- else if (strcmp(command, "FWINX") == 0) {
- printf("Der Spieler hat gewonnen!");
- running = false;
- }
- else if(strcmp(command, "FWINO") == 0) {
- printf("Der Computer hat gewonnen!");
- running = false;
- }
- else if(strcmp(command, "FDRAW") == 0) {
- printf("Das Spiel endete unentschieden.");
- running = false;
- }
- else if(strcmp(command, "ERROR") == 0) {
- printf("Zug ungültig.");
- running = false;
- }
- }
- close(sock);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement