/* tcptoe.c * Tic-tac-toe game server * By Feky */ #include #include #include #include #include #define MAXBUF 106 #define PLC 3 #define SOCK_ERR -1 char table[MAXBUF]=" | | \n | | \n ---|---|---\n | | \n" " ---|---|---\n | | \n | | \n\n"; /* table */ int pos[10]={19, 23, 27, 49, 53, 57, 79, 83, 87, 87}; /* positions in the table */ int x(int sock){ /* server's turn */ char c[PLC]; int i; a: fgets(c, PLC, stdin); i = atoi(c); if(table[pos[i-1]]==*" " && (i < 10 && i > 0)){ table[pos[i-1]]=*"X"; printf("%s", table); write(sock, table, MAXBUF); } else goto a; } int o(int sock){ /* client's turn */ char c[PLC]; int i; a: read(sock, c, PLC-1); i = atoi(c); if(table[pos[i-1]]==*" " && (i < 10 && i > 0)){ table[pos[i-1]]=*"O"; write(sock, table, MAXBUF); printf("%s\n", table); } else goto a; } int chk(){ /* is there a winner yet? */ /* the server? */ if ((table[19]==*"X"&&table[23]==*"X"&&table[27]==*"X") || (table[49]==*"X"&&table[53]==*"X"&&table[57]==*"X")) return 1; if ((table[79]==*"X"&&table[83]==*"X"&&table[87]==*"X") || (table[19]==*"X"&&table[49]==*"X"&&table[79]==*"X")) return 1; if ((table[23]==*"X"&&table[53]==*"X"&&table[83]==*"X") || (table[27]==*"X"&&table[57]==*"X"&&table[87]==*"X")) return 1; if ((table[19]==*"X"&&table[53]==*"X"&&table[87]==*"X") || (table[27]==*"X"&&table[53]==*"X"&&table[79]==*"X")) return 1; /* or the client? */ if ((table[19]==*"O"&&table[23]==*"O"&&table[27]==*"O") || (table[49]==*"O"&&table[53]==*"O"&&table[57]==*"O")) return 2; if ((table[79]==*"O"&&table[83]==*"O"&&table[87]==*"O") || (table[19]==*"O"&&table[49]==*"O"&&table[79]==*"O")) return 2; if ((table[23]==*"O"&&table[53]==*"O"&&table[83]==*"O") || (table[27]==*"O"&&table[57]==*"O"&&table[87]==*"O")) return 2; if ((table[19]==*"O"&&table[53]==*"O"&&table[87]==*"O") || (table[27]==*"O"&&table[53]==*"O"&&table[79]==*"O")) return 2; /* nobody? (draw game) */ if (table[19]!=*" "&&table[23]!=*" "&&table[27]!=*" "&&table[49]!=*" "&&table[53]!=*" "&&table[57]!=*" "&&table[79]!=*" "&&table[83]!=*" "&&table[87]!=*" ") return 3; else return 0; } int main(int argc, char* argv[]){ int sockfd, new_sockfd; struct sockaddr_in server_addr, client_addr; socklen_t addrlen; int port; if(argc != 2){ /* check parameters */ printf("Usage: %s [0-65535]\n", argv[0]); return 1; } port = atoi(argv[1]); if(port < 0 || port > 65535){ /* it must be a valid port number */ printf("The port must be between 0 and 65535.\n"); return 1; } /* make a socket */ if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == SOCK_ERR){ printf("Can't make socket.\n"); return 1; } server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr.s_addr = INADDR_ANY; /* bind */ if(bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == SOCK_ERR){ printf("Can't bind to port %d. Call me a bitch.\n", port); return 1; } /* listen */ if(listen(sockfd, 10) == SOCK_ERR){ printf("Can't listen.\n"); return 1; } printf("\n* Server launched.\n* Listening on port %d.\n", port); /* accept the connection */ addrlen = sizeof(client_addr); new_sockfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen); printf("* Client(%s) connected.\n\n", inet_ntoa(client_addr.sin_addr)); write(new_sockfd, "\n* Welcome to tcptoe server! *\n", 32); while (chk() == 0){ /* play */ x(new_sockfd); if(chk() != 0) break; o(new_sockfd); } switch(chk()){ /* long live the winner! */ case 1: printf("X wins!\n"); write(new_sockfd, "X Wins!\n", 9); break; case 2: printf("O wins!\n"); write(new_sockfd, "O Wins!\n", 9); break; case 3: printf("Draw game!\n"); write(new_sockfd, "Draw game!\n", 12); break; } close(sockfd); return 0; }