Advertisement
Th3R3v0lut1on

Prog

Nov 5th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.10 KB | None | 0 0
  1. #include <stdbool.h>     // use booleans in C
  2. #include <stdlib.h>      // use common functions
  3. #include <stdio.h>       // use input/output functions
  4. #include <string.h>      // use string functions
  5. #include <unistd.h>      // use POSIX functions
  6.  
  7. #include <sys/socket.h>  // use socket function
  8. #include <netinet/in.h>  // use internet address datatypes
  9. #include <arpa/inet.h>   // use inet_ntoa function
  10. #include <netdb.h>       // use network database functions
  11.  
  12. /** Print field represented by given character array.
  13.     The array must contains 9 chars ('O', 'X' or ' ')
  14.     ordered row-wise from top left to bottom right. */
  15. void printField(char* field)
  16. {
  17.     printf("+---+---+---+\n");
  18.     for (int row = 0; row < 3; row++) {
  19.         for (int col = 0; col < 3; col++) {
  20.             printf("| %c ", field[row*3+col]);
  21.         }
  22.         printf("|\n+---+---+---+\n");
  23.     }
  24. }
  25.  
  26. /** Simple client application for Tic-Tac-Toe game using UDP.
  27.     Hostname and port number of server are passed as arguments.
  28.     By default, the server is assumed at localhost, port 5001.
  29. */
  30. int main(int argc, char* argv[])
  31. {
  32.     // Read parameters
  33.     if (argc != 3) {
  34.         printf("Usage: TicTacToeClient [<hostname>] [<port>]\n\n");
  35.     }
  36.     char* hostname = argc > 1 ? argv[1] : "localhost";
  37.     int port = argc > 2 ? atoi(argv[2]) : 5001;
  38.  
  39.     /** -- Setup connection -- */
  40.     //Set server port
  41.     struct hostent* serverInfo = gethostbyname(hostname);
  42.     if (serverInfo == NULL) {
  43.         printf("Failed to get info for host '%s'!\n", hostname);
  44.         return -1;
  45.     }
  46.  
  47.     // Create UDP/IP socket
  48.     int sock = socket(AF_INET, SOCK_DGRAM, 0);
  49.     if (sock < 0) {
  50.         printf("Failed to create socket!\n");
  51.         return -1;
  52.     }
  53.  
  54.    
  55.     // Set address and port of server
  56.     struct sockaddr_in serverAddr;
  57.     serverAddr.sin_family = serverInfo->h_addrtype;
  58.     serverAddr.sin_port = htons(port);
  59.     memcpy(&serverAddr.sin_addr.s_addr, serverInfo->h_addr, serverInfo->h_length);
  60.     unsigned int addrLen = sizeof(serverAddr);
  61.  
  62.     printf("Expecting server at %s:%d\n\n",
  63.            inet_ntoa(serverAddr.sin_addr), ntohs(serverAddr.sin_port));
  64.  
  65.  
  66.     /** -- Main game -- */
  67.  
  68.     bool running = true;
  69.     char message[16];
  70.     char command[6];
  71.     char field[9];
  72.     memset(field, ' ', 9);
  73.     printField(field);
  74.     while (running)
  75.     {
  76.         // Read row and column from user
  77.         int row = 0, col = 0;
  78.         printf("Select row and column (1-3): ");
  79.         scanf("%d %d", &row, &col);
  80.         while (getchar() != '\n') {}  // discard remaining input
  81.         if (row < 1 || row > 3 || col < 1 || col > 3) {
  82.             printf("Invalid row or column!\n");
  83.             continue;
  84.         }
  85.  
  86.         // Update field
  87.         int idx = (row-1)*3 + col-1;
  88.         if (field[idx] != ' ') {
  89.             printf("This field is already occupied!\n");
  90.             continue;
  91.         }
  92.         field[idx] = 'X';
  93.         printField(field);
  94.        
  95.         // TODO : Send message to server, receive and process response
  96.         memset(message, 0, 16);
  97.         strncpy(command, message, 6);
  98.         strncpy(field, message+6, 9);
  99.         message[16] = '\0';
  100.        
  101.         // Send message to server
  102.         if (sendto(sock, message, 16, 0, (struct sockaddr*)&serverAddr, addrLen) < 0) {
  103.             printf("Failed to send message to server!\n");
  104.             running = false;
  105.             continue;
  106.         } else {
  107.             printf("Sent message to server: \"%s\"\n", message);
  108.         }
  109.        
  110.         // Receive response from server
  111.         printf("Waiting for response...\n");
  112.         memset(message, 0, 16);
  113.         if (recvfrom(sock, message, 1024, 0, (struct sockaddr*)&serverAddr, &addrLen) < 0) {
  114.             printf("Failed to receive response from server!\n");
  115.             running = false;
  116.             continue;
  117.         }
  118.        
  119.        
  120.         memcpy(command, message, 5);
  121.         command[5] = '\0';
  122.         memcpy(field, message + 6, 9);
  123.        
  124.         // Check message and field and respond
  125.         if (strcmp(command, "FIELD") == 0) {
  126.             printField(field);
  127.             continue;
  128.         }
  129.         else if (strcmp(command, "FWINX") == 0) {
  130.             printf("Der Spieler hat gewonnen!");
  131.             running = false;
  132.         }
  133.         else if(strcmp(command, "FWINO") == 0) {
  134.             printf("Der Computer hat gewonnen!");
  135.             running = false;
  136.         }
  137.         else if(strcmp(command, "FDRAW") == 0) {
  138.             printf("Das Spiel endete unentschieden.");
  139.             running = false;
  140.         }
  141.         else if(strcmp(command, "ERROR") == 0) {
  142.             printf("Zug ungültig.");
  143.             running = false;
  144.         }
  145.     }
  146.  
  147.     close(sock);
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement