message_t getIdAndPassword(int mesgType) { char id[MAXLENGTH], *pass; printf("ID: "); fgets(id, 1000, stdin); id[strlen(id)-1] = '\0'; pass = getpass("Password: "******"0", "0"); }
void str_cli(int sockfd) { char *sendline, recvline[MESSAGE_MAXLEN], opt[100]; int ch; char option[100]; message_t newMesg; fd_set readset; char invite[MAXLENGTH]; while(1) { ch = menu(); switch(ch) { case 1: printf("\n\n----------------------------------------------------------------------------------------\nSIGN IN\n"); sendline = message_toString(getIdAndPassword(SIGNIN)); if (send(sockfd, sendline, MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) { printf("send() sent a different number of bytes than expected\n"); } if (recv(sockfd, recvline, MESSAGE_MAXLEN, 0) < 0) { printf("Server terminated prematurely\n"); exit(1); } recvline[strlen(recvline)] = '\0'; newMesg = message_parse(recvline); newMesg->arg[1] = '\0'; newMesg->arg1[1] = '\0'; switch(newMesg->cmd) { case SIGNIN: printf("\nWelcome to console chess game\n"); menuPlay(); while(1) { FD_ZERO(&readset); FD_SET(sockfd, &readset); FD_SET(fileno(stdin), &readset); select(max(sockfd, fileno(stdin)) + 1, &readset, NULL, NULL, NULL); if(FD_ISSET(sockfd, &readset)) { if(read(sockfd, recvline, MESSAGE_MAXLEN) == 0) { printf("Server terminated prematurely\n"); exit(1); } //TODO: receive invitation newMesg = message_parse(recvline); switch(newMesg->cmd) { case INVITE_ACCEPT: printf("Client %s invited you to his/her game (y/n) ?\n", newMesg->arg1); scanf("%s", opt); if(strcmp(opt, "y") == 0 || strcmp(opt, "Y") == 0) { if (send(sockfd, recvline, MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } } else { if (send(sockfd, message_toString( message_construct(INVITE_DECLINE, newMesg->arg1, "0", "0", "0") ), MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } } break; case PICK_SIDE: if(strcmp(newMesg->arg, "0") == 0) { mainConsoleChess(0, sockfd); menuPlay(); } else if (strcmp(newMesg->arg, "1") == 0) { mainConsoleChess(1, sockfd); menuPlay(); } case GET_PLAYERLIST: parse(newMesg->arg); menuPlay(); break; default: printf("Cannot get the message\n"); break; } } else if(FD_ISSET(fileno(stdin), &readset)) { fgets(option, 100, stdin); option[strlen(option) -1] = '\0'; if(strcmp(option, "1") == 0) { //TODO: inviting someone printf("Type the one'id who you want to invite\n"); scanf("%s", invite); if (send(sockfd, message_toString(message_construct(INVITE_SEND, invite, "0", "0", "0")), MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) { printf("send() sent a different number of bytes than expected\n"); } printf("Waiting for responding from server...\n"); if (recv(sockfd, recvline, MESSAGE_MAXLEN, 0) < 0) { printf("Server terminated prematurely\n"); exit(1); } recvline[strlen(recvline)] = '\0'; newMesg = message_parse(recvline); newMesg->arg[1] = '\0'; if(newMesg->cmd == PICK_SIDE) { if(strcmp(newMesg->arg, "0") == 0) { mainConsoleChess(0, sockfd); menuPlay(); } else if (strcmp(newMesg->arg, "1") == 0) { mainConsoleChess(1, sockfd); menuPlay(); } } else { printf("He/she declined your request\n"); menuPlay(); } } else if(strcmp(option, "2") == 0) { //TODO: get the current players list from server if (send(sockfd, message_toString(message_construct(GET_PLAYERLIST, "0", "0", "0", "0")), MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) { printf("send() sent a different number of bytes than expected\n"); } printf("Waiting for responding from server...\n"); } else if(strcmp(option, "3") == 0) { printf("Thanks for enjoying\n"); break; } else { printf("Not the right option, baby\n"); menuPlay(); continue; } } } break; default: printf("MESSAGE COMMAND: %d\n", newMesg->cmd); printf("Fail to sign in\n"); break; } continue; case 2: printf("\n\n----------------------------------------------------------------------------------------\nSIGN UP\n"); sendline = message_toString(getIdAndPassword(SIGNUP)); if (send(sockfd, sendline, MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } if (recv(sockfd, recvline, MESSAGE_MAXLEN, 0) < 0) { printf("str_cli: server terminated prematurely\n"); exit(1); } recvline[strlen(recvline)] = '\0'; newMesg = message_parse(recvline); switch(newMesg->cmd) { case SIGNUP: printf("\nSuccessfull sign up\n"); break; default: printf("Fail to sign up. Maybe this account doesn't exist\n"); break; } continue; case 3: printf("----------------------------------------------------------------------------------------\nExit\n"); sendline = message_toString(message_construct(SIGNOUT, "0", "0", "0", "0")); if (send(sockfd, sendline, MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) { printf("send() sent a different number of bytes than expected\n"); } if (recv(sockfd, recvline, MESSAGE_MAXLEN, 0) < 0) { printf("Server terminated prematurely\n"); exit(1); } recvline[strlen(recvline)] = '\0'; printf("\nI'm done!\n"); exit(0); break; default: printf("Wrong option! Please choose again\n"); continue; } break; } printf("\n"); }
message_t message_parse (char msg[MESSAGE_MAXLEN]) { /** * Detect existing fields */ int cmd = 0; char *cmd_appear = strstr(msg, CMD_DECL); if (cmd_appear != NULL) { char *pch = strchr (cmd_appear, ';'); char *cmd_str = NULL; cmd_str = (char *) malloc (COMP_MAXLEN); strncpy(cmd_str, cmd_appear+strlen(CMD_DECL), pch-cmd_appear-strlen(CMD_DECL)); cmd = atoi(cmd_str); free(cmd_str); } else { perror("CMD must not be NULL"); } char *arg = NULL; char *arg_appear = strstr(msg, ARG_DECL); if (arg_appear != NULL) { char *pch = strchr (arg_appear, ';'); arg = (char *) malloc (COMP_MAXLEN); strncpy(arg, arg_appear+strlen(ARG_DECL), pch-arg_appear-strlen(ARG_DECL)); arg[pch-arg_appear-strlen(ARG_DECL)] = '\0'; } else { arg = (char *) malloc (1); strcpy(arg, ""); } char *arg1 = NULL; char *arg1_appear = strstr(msg, ARG1_DECL); if (arg1_appear != NULL) { char *pch = strchr (arg1_appear, ';'); arg1 = (char *) malloc (COMP_MAXLEN); strncpy(arg1, arg1_appear+strlen(ARG1_DECL), pch-arg1_appear-strlen(ARG1_DECL)); arg1[pch-arg1_appear-strlen(ARG1_DECL)] = '\0'; } else { arg1 = (char *) malloc (1); strcpy (arg1, ""); } char *option = NULL; char *option_appear = strstr (msg, OPTION_DECL); if (option_appear != NULL) { char *pch = strchr (option_appear, ';'); option = (char *) malloc (COMP_MAXLEN); strncpy (option, option_appear+strlen(OPTION_DECL), pch-option_appear-strlen(OPTION_DECL)); option[pch-option_appear-strlen(OPTION_DECL)] = '\0'; } else { option = (char *) malloc (1); strcpy (option, ""); } char *option1 = NULL; char *option1_appear = strstr (msg, OPTION1_DECL); if (option1_appear != NULL) { char *pch = strchr (option1_appear, ';'); option1 = (char *) malloc (COMP_MAXLEN); strncpy (option1, option1_appear+strlen(OPTION1_DECL), pch-option1_appear-strlen(OPTION1_DECL)); option1[pch-option1_appear-strlen(OPTION1_DECL)] = '\0'; } else { option1 = (char *) malloc (1); strcpy (option1, ""); } /** * Make new message */ return message_construct (cmd, arg, arg1, option, option1); }
int main(){ int serv_sockfd, client_sockfd; int server_len, client_len, bytes_received; struct sockaddr_in server_addr; struct sockaddr_in client_addr; char recv_data[1024], send_data[1024], recvdata[1024]; int result; int fd; int nread; struct timeval tv; fd_set fdset, testfds; char id[MAXLENGTH], password[MAXLENGTH]; serv_sockfd = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_ANY); server_addr.sin_port = htons(1234); server_len = sizeof(server_addr); //Create a TCP/IP socket to use if ((serv_sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Socket() error"); exit(1); } if (bind(serv_sockfd, (struct sockaddr *)&server_addr, server_len)== -1) { perror("bind() error"); exit(1); } if (listen(serv_sockfd, 5) == -1) { perror("Listen() error"); exit(1); } FD_ZERO(&fdset); /*clear all bits in fdset*/ FD_SET(serv_sockfd, &fdset); /*turn on the bit for fd in fdset*/ while(1) { /* wait until either socket has data ready to be recv()d (timeout 5.5 secs)*/ tv.tv_sec = 5; tv.tv_usec = 500000; testfds = fdset; printf("Server waiting\n"); result = select(FD_SETSIZE, &testfds, (fd_set *)NULL,(fd_set *)NULL, &tv); if(result == -1) { perror("Select() error"); // error occurred in select() exit(1); } else if(result == 0){ printf("Timeout occurred! No data after 5.5 seconds.\n"); } else{ //Client connected for(fd = 0; fd < FD_SETSIZE; fd++){ if(FD_ISSET(fd,&testfds)) { if(fd == serv_sockfd){ printf("Request on port %d\n", fd); client_len = sizeof(client_addr); client_sockfd = accept(serv_sockfd,(struct sockaddr *)&client_addr, &client_len); FD_SET(client_sockfd, &fdset); printf("Client %d connected !\n", client_sockfd); } else { ioctl(fd, FIONREAD, &nread); if(nread != 0) { printf("\nHandling on socket %d\n",fd); //TODO: Handle socket here bytes_received = recv(fd,recv_data,1024,0); recv_data[bytes_received] = '\0'; message_t newMesg = message_parse(recv_data); switch(newMesg->cmd) { case SIGNIN: strcpy(id, newMesg->arg); printf("%s\n", id); strcpy(password, newMesg->arg1); printf("%s\n", password); //TODO: check sign in here if(1) { if (send(fd, recv_data, strlen(recv_data), 0) != strlen(recv_data)) { printf("send() sent a different number of bytes than expected\n"); } //TODO: play module here } else { if (send(fd, message_toString( message_construct(FAILURE, "0", "0", "0", "0") ), 1024, 0) != 1024) { printf("send() sent a different number of bytes than expected\n"); } } break; case SIGNUP: strcpy(id, newMesg->arg); printf("%s\n", id); strcpy(password, newMesg->arg1); printf("%s\n", password); //TODO Check account here if(1) { if (send(fd, recv_data, strlen(recv_data), 0) != strlen(recv_data)) { printf("send() sent a different number of bytes than expected\n"); } } else { if (send(fd, message_toString( message_construct(FAILURE, "0", "0", "0", "0") ), 1024, 0) != 1024) { printf("send() sent a different number of bytes than expected\n"); } } break; case SIGNOUT: // TODO: decrease player counter if (send(fd, recv_data, strlen(recv_data), 0) != strlen(recv_data)) { printf("send() sent a different number of bytes than expected\n"); } break; default: printf("Error occurred\n"); if (send(fd, message_toString( message_construct(FAILURE, "0", "0", "0", "0") ), 1024, 0) != 1024) { printf("send() sent a different number of bytes than expected\n"); } break; } } else { close(fd); FD_CLR(fd, &fdset); /*turn off the bit for fd in fdset*/ printf("Not connect client on fd %d\n", fd); } } } } } } close(serv_sockfd); }
int main () { gameserver_core_init (); gameserver_core_loadUsers (); /** * Server connection handling */ char temp[1024]; int enemySide, otherSide; int randomNum, userCounter = 0; int serv_sockfd, client_sockfd; int bytes_received; socklen_t server_len, client_len; struct sockaddr_in server_addr; struct sockaddr_in client_addr; char recv_data[1024]; int result; int fd; int nread; struct timeval tv; fd_set fdset, testfds; serv_sockfd = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_ANY); server_addr.sin_port = htons(1234); server_len = sizeof(server_addr); //Create a TCP/IP socket to use if ((serv_sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Socket() error"); exit(1); } if (bind(serv_sockfd, (struct sockaddr *)&server_addr, server_len)== -1) { perror("bind() error"); exit(1); } if (listen(serv_sockfd, 5) == -1) { perror("Listen() error"); exit(1); } FD_ZERO(&fdset); /*clear all bits in fdset*/ FD_SET(serv_sockfd, &fdset); /*turn on the bit for fd in fdset*/ while(1) { /* wait until either socket has data ready to be recv()d (timeout 5.5 secs)*/ tv.tv_sec = 5; tv.tv_usec = 500000; testfds = fdset; result = select(FD_SETSIZE, &testfds, (fd_set *)NULL,(fd_set *)NULL, &tv); if(result == -1) { perror("Select() error"); // error occurred in select() exit(1); } else if(result == 0){ // printf("Timeout occurred! No data after 5.5 seconds.\n"); } else{ //Client connected for(fd = 0; fd < FD_SETSIZE; fd++){ if(FD_ISSET(fd,&testfds)) { if(fd == serv_sockfd){ printf("Request on port %d\n", fd); client_len = sizeof(client_addr); client_sockfd = accept(serv_sockfd,(struct sockaddr *)&client_addr, &client_len); FD_SET(client_sockfd, &fdset); printf("Client %d connected !\n", client_sockfd); } else { ioctl(fd, FIONREAD, &nread); if(nread != 0) { printf("\nHandling on socket %d\n",fd); // Handle socket here bytes_received = recv(fd,recv_data,1024,0); recv_data[bytes_received] = '\0'; message_t newMesg = message_parse(recv_data); /** * Game server business logic */ int response = 0; char logStr[100]; switch(newMesg->cmd) { case SIGNIN: { sprintf (logStr, "User signing in at port %d", fd); logging (logStr); response = gameserver_core_signin (newMesg->arg, newMesg->arg1, fd); if (response >= 0) { if (send(fd, recv_data, MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) { printf("send() sent a different number of bytes than expected\n"); } userCounter++; gameserver_core_signin(newMesg->arg, newMesg->arg1, fd); sprintf (logStr, "User %s signed in at port %d", newMesg->arg, fd); logging (logStr); } else { printf("SEND THIS COMMAND: %d\n", FAILURE); if (send(fd, message_toString( message_construct(FAILURE, "0", "0", "0", "0") ), MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } } } break; case SIGNUP: response = gameserver_core_signup (newMesg->arg, newMesg->arg1); if(response > 0) { if (send(fd, recv_data, MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) { printf("send() sent a different number of bytes than expected\n"); } gameserver_core_signup(newMesg->arg, newMesg->arg1); sprintf (logStr, "User %s signup in at port %d", newMesg->arg, fd); logging (logStr); } else { if (send(fd, message_toString( message_construct(FAILURE, "0", "0", "0", "0") ), MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } } break; case SIGNOUT: // TODO: decrease player counter if (send(fd, recv_data, MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { sprintf (logStr, "User %s signed out at port %d", newMesg->arg, fd); logging (logStr); printf("send() sent a different number of bytes than expected\n"); } userCounter--; break; case MOVE: //Check the port here enemySide = gameserver_core_getOtherUserSockfd(fd); if (send(enemySide, recv_data, MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { sprintf (logStr, "User moved %s in at port %d", newMesg->arg, fd); logging (logStr); printf("send() sent a different number of bytes than expected\n"); } break; case INVITE_SEND: //send it to other socket newMesg->arg[1] = '\0'; enemySide = atoi(newMesg->arg); sprintf(temp, "%d", fd); if (send(enemySide, message_toString( message_construct(INVITE_ACCEPT, newMesg->arg, temp, "0", "0") ), MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { sprintf (logStr, "User at port %d sent invite to user at socket %s", fd, newMesg->arg); logging (logStr); printf("send() sent a different number of bytes than expected\n"); } break; case INVITE_ACCEPT: newMesg->arg[1] = '\0'; newMesg->arg1[1] = '\0'; enemySide = atoi(newMesg->arg); otherSide = atoi(newMesg->arg1); srand(time(NULL)); randomNum = rand() % 2; sprintf(temp, "%d", randomNum); if (send(enemySide, message_toString( message_construct(PICK_SIDE, temp, "0", "0", "0") ), MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } sprintf(temp, "%d", randomNum ^ 1); if (send(otherSide, message_toString( message_construct(PICK_SIDE, temp, "0", "0", "0") ), MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } //TODO: change status of user here sprintf (logStr, "Challege accepted :-< Battle has begun %d vs. %d", enemySide, otherSide); logging (logStr); gameserver_core_initNewRoom(enemySide, otherSide); break; case INVITE_DECLINE: newMesg->arg[1] = '\0'; newMesg->arg1[1] = '\0'; if (send(atoi(newMesg->arg), recv_data, MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { sprintf (logStr, "User %d declined", fd); logging (logStr); printf("send() sent a different number of bytes than expected\n"); } break; case RESULT: if (send(fd, message_toString( message_construct(SIGNOUT, "0", "0", "0", "0")), MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } enemySide = gameserver_core_getOtherUserSockfd(fd); if (send(enemySide, message_toString( message_construct(SIGNOUT, "0", "0", "0", "0")), MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } sprintf (logStr, "The war is over :-< "); logging (logStr); break; case GET_PLAYERLIST: if (send(fd, message_toString( message_construct(GET_PLAYERLIST, gameserver_core_getPlayerlist(), "0", "0", "0")), MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } sprintf (logStr, "User %d requested player list", fd); logging (logStr); break; default: printf("Error occurred\n"); if (send(fd, message_toString( message_construct(FAILURE, "0", "0", "0", "0") ), MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } break; } } else { close(fd); FD_CLR(fd, &fdset); /*turn off the bit for fd in fdset*/ printf("Not connect client on fd %d\n", fd); userCounter--; //TODO change user status here enemySide = gameserver_core_getOtherUserSockfd(fd); // if (send(enemySide, message_toString( message_construct(RESULT, "1/2", "1/2", "0", "0") ) , MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { // printf("send() sent a different number of bytes than expected\n"); // } } } } } } } gameserver_core_endSession(); close(serv_sockfd); }
int main (int argc, char **argv) { char const *doctype; char const **ptr; args_data = postqueue_data = message_data; message_construct(argv[0]); args_construct(argc, argv, "?x:l:", 0); hashmap_construct(&symtab); lines_construct(&global.d_toc); lines_construct(&global.d_section); lines_add(&global.d_section, ""); hashmap_constructText(&global.d_symbol, default_symbols); if (!args_ok() || args_nArgs() < 2) /* check arguments */ usage(); if (args_nArgs() == 2) /* file name specified */ { global.d_out = stdout; global.d_noext = 0; } else { global.d_noext = file_rmExtension(args_arg(2)); global.d_ext = file_extension(args_arg(2)); if (!global.d_ext) { global.d_ext = new_str(args_optarg('x')); if (!global.d_ext) global.d_ext = "ypp"; /* Yodl Post Processor */ } } string_construct(&global.d_outName, 0); postqueue_construct(task); if (global.d_noext) { string_format(&global.d_outName, "%s.%s", global.d_noext, global.d_ext); global.d_out = file_open(string_str(&global.d_outName), "w"); } doctypes[sizeofDocType - 1] = doctype = hashmap_textOf(&global.d_symbol, "documenttype"); for ( ptr = doctypes, global.d_doctype = 1; strcmp(doctype, *ptr); ptr++, global.d_doctype <<= 1 ) ; postqueue_process(); fclose(global.d_out); return 0; }
int main(){ //FOR DEBUGGING char id1[100] = "Hung", password1[100] = "123456", id2[100] = "Huy", password2[100] = "123456", temp[100]; int socket1, socket2; int serv_sockfd, client_sockfd; int server_len, client_len, bytes_received; struct sockaddr_in server_addr; struct sockaddr_in client_addr; char recv_data[1024], send_data[1024], recvdata[1024]; int result; int fd; int nread; struct timeval tv; fd_set fdset, testfds; char id[MAXLENGTH], password[MAXLENGTH]; serv_sockfd = socket(AF_INET, SOCK_STREAM, 0); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_ANY); server_addr.sin_port = htons(1234); server_len = sizeof(server_addr); //Create a TCP/IP socket to use if ((serv_sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Socket() error"); exit(1); } if (bind(serv_sockfd, (struct sockaddr *)&server_addr, server_len)== -1) { perror("bind() error"); exit(1); } if (listen(serv_sockfd, 5) == -1) { perror("Listen() error"); exit(1); } FD_ZERO(&fdset); /*clear all bits in fdset*/ FD_SET(serv_sockfd, &fdset); /*turn on the bit for fd in fdset*/ while(1) { /* wait until either socket has data ready to be recv()d (timeout 5.5 secs)*/ tv.tv_sec = 5; tv.tv_usec = 500000; testfds = fdset; printf("Server waiting\n"); result = select(FD_SETSIZE, &testfds, (fd_set *)NULL,(fd_set *)NULL, &tv); if(result == -1) { perror("Select() error"); // error occurred in select() exit(1); } else if(result == 0){ printf("Timeout occurred! No data after 5.5 seconds.\n"); } else{ //Client connected for(fd = 0; fd < FD_SETSIZE; fd++){ if(FD_ISSET(fd,&testfds)) { if(fd == serv_sockfd){ printf("Request on port %d\n", fd); client_len = sizeof(client_addr); client_sockfd = accept(serv_sockfd,(struct sockaddr *)&client_addr, &client_len); FD_SET(client_sockfd, &fdset); printf("Client %d connected !\n", client_sockfd); } else { ioctl(fd, FIONREAD, &nread); if(nread != 0) { printf("\nHandling on socket %d\n",fd); //TODO: Handle socket here bytes_received = recv(fd,recv_data,1024,0); recv_data[bytes_received] = '\0'; puts(recv_data); message_t newMesg = message_parse(recv_data); switch(newMesg->cmd) { case SIGNIN: strcpy(id, newMesg->arg); printf("The ID: %s\nID 1: %s\nID 2: %s\n", id, id1, id2); //DEBUGGING strcpy(password, newMesg->arg1); printf("The password: %s\nPassword 1: %s\nPassword 2: %s", password, password1, password2); //DEBUGGING //TODO: check sign in here //FOR DEBUGGING printf("%s\n", message_toString(newMesg)); if((strcmp(id, id1) == 0) && (strcmp(password, password1) == 0)) { printf("this's true\nPICKSIDE COMMAND: %d", PICK_SIDE); socket1 = fd; sprintf(temp, "%d", socket1); printf("%s\n", message_toString(message_construct(PICK_SIDE, "1", temp, "0", "0"))); if (send(fd, message_toString(message_construct(PICK_SIDE, "1", temp, "0", "0")), MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) { printf("send() sent a different number of bytes than expected\n"); } } else if((strcmp(id, id2) == 0) && (strcmp(password, password2) == 0)) { socket2 = fd; sprintf(temp, "%d", socket1); printf("%s\n", message_toString(message_construct(PICK_SIDE, "0", temp, "0", "0"))); if (send(fd, message_toString(message_construct(PICK_SIDE, "0", temp, "0", "0")), MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) { printf("send() sent a different number of bytes than expected\n"); } } //END DEBUGGING else { printf("SEND THIS COMMAND: %d\n", FAILURE); if (send(fd, message_toString( message_construct(FAILURE, "0", "0", "0", "0") ), MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } } break; case SIGNUP: strcpy(id, newMesg->arg); printf("%s\n", id); strcpy(password, newMesg->arg1); printf("%s\n", password); //TODO Check account here if(1) { if (send(fd, recv_data, MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) { printf("send() sent a different number of bytes than expected\n"); } } else { if (send(fd, message_toString( message_construct(FAILURE, "0", "0", "0", "0") ), MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } } break; case SIGNOUT: // TODO: decrease player counter if (send(fd, recv_data, MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } break; case MOVE: //TODO: play module here //FOR DEBUGGING if(fd == socket1) { printf("%d turn\n", socket1); //Check the port here if (send(socket2, recv_data, MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } } else { printf("%d turn\n", socket1); if (send(socket1, recv_data, MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } } //END DEBUGGING break; default: printf("Error occurred\n"); if (send(fd, message_toString( message_construct(FAILURE, "0", "0", "0", "0") ), MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } break; } } else { close(fd); FD_CLR(fd, &fdset); /*turn off the bit for fd in fdset*/ printf("Not connect client on fd %d\n", fd); } } } } } } close(serv_sockfd); }
void str_cli(int sockfd) { char *sendline, recvline[MAXLINE], ch; message_t newMesg, recvMesg; int buffrecv; while(1) { ch = menu(); switch(ch) { case '1': printf("\n\nSIGN IN\n"); sendline = message_toString(getIdAndPassword(SIGNIN)); if (send(sockfd, sendline, MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) { printf("send() sent a different number of bytes than expected\n"); } if (recv(sockfd, recvline, MESSAGE_MAXLEN, 0) < 0) { printf("str_cli: server terminated prematurely\n"); exit(1); } recvline[strlen(recvline)] = '\0'; newMesg = message_parse(recvline); switch(newMesg->cmd) { case SIGNIN: printf("\nWelcome to console chess game\n"); for(;;) { } mainConsoleChess(LIGHT, sockfd); break; default: printf("Fail to sign in\n"); break; } continue; case '2': printf("\n\nSIGN UP\n"); sendline = message_toString(getIdAndPassword(SIGNUP)); if (send(sockfd, sendline, MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } if (recv(sockfd, recvline, MESSAGE_MAXLEN, 0) < 0) { printf("str_cli: server terminated prematurely\n"); exit(1); } recvline[strlen(recvline)] = '\0'; puts(recvline); //Debug newMesg = message_parse(recvline); switch(newMesg->cmd) { case SIGNUP: //printf("\nSuccessfull sign up id %s.\n", newMesg->arg); printf("\nSuccessfull sign up\n"); break; default: printf("Fail to sign up. Maybe this account doesn't exist\n"); break; } continue; case '3': printf("Exit\n", ch); sendline = message_toString(message_construct(SIGNOUT, "0", "0", "0", "0")); if (send(sockfd, sendline, MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) { printf("send() sent a different number of bytes than expected\n"); } if (buffrecv = recv(sockfd, recvline, MESSAGE_MAXLEN, 0) < 0) { printf("str_cli: server terminated prematurely\n"); exit(1); } recvline[strlen(recvline)] = '\0'; puts(recvline); //Debug printf("\nI'm done!\n"); exit(0); break; default: printf("Wrong option! Please choose again\n"); continue; } break; } printf("\n"); }
void str_cli(int sockfd) { char *sendline, recvline[MAXLINE], ch; message_t newMesg, recvMesg; int buffrecv; while(1) { ch = menu(); switch(ch) { case '1': printf("\n\nSIGN IN\n"); sendline = message_toString(getIdAndPassword(SIGNIN)); if (send(sockfd, sendline, MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) { printf("send() sent a different number of bytes than expected\n"); } if (recv(sockfd, recvline, MESSAGE_MAXLEN, 0) < 0) { printf("str_cli: server terminated prematurely\n"); exit(1); } recvline[strlen(recvline)] = '\0'; //printf("%d\n", strlen(recvline)); puts(recvline); newMesg = message_parse(recvline); printf("COMMAND: %d\nArg: %s\nArg1: %s\nOption: %s\nOption1: %s\n", newMesg->cmd, newMesg->arg, newMesg->arg1, newMesg->option, newMesg->option1); puts("{ cmd : 9; arg : 1; arg1 : 5; option : 0; option1 : 0; }"); newMesg = message_parse("{ cmd : 9; arg : 1; arg1 : 5; option : 0; option1 : 0; }"); if(strcmp(recvline, "{ cmd : 9; arg : 1; arg1 : 5; option : 0; option1 : 0; }") == 0) { printf("COMMAND: %d\nArg: %s\nArg1: %s\nOption: %s\nOption1: %s\n", newMesg->cmd, newMesg->arg, newMesg->arg1, newMesg->option, newMesg->option1); printf("Debugging\n"); } printf("MESSAGE COMMAND: %d\n", newMesg->cmd); switch(newMesg->cmd) { case PICK_SIDE: printf("\nWelcome to console chess game\n"); //printf("Arg: %s\n", newMesg->arg); if(strcmp(newMesg->arg, "0") == 0) { mainConsoleChess(0, sockfd); } else if (strcmp(newMesg->arg, "1") == 0) { mainConsoleChess(1, sockfd); } break; default: printf("MESSAGE COMMAND: %d\n", newMesg->cmd); printf("Fail to sign in\n"); break; } continue; case '2': printf("\n\nSIGN UP\n"); sendline = message_toString(getIdAndPassword(SIGNUP)); if (send(sockfd, sendline, MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN ) { printf("send() sent a different number of bytes than expected\n"); } if (recv(sockfd, recvline, MESSAGE_MAXLEN, 0) < 0) { printf("str_cli: server terminated prematurely\n"); exit(1); } recvline[strlen(recvline)] = '\0'; puts(recvline); //Debug newMesg = message_parse(recvline); switch(newMesg->cmd) { case SIGNUP: //printf("\nSuccessfull sign up id %s.\n", newMesg->arg); printf("\nSuccessfull sign up\n"); break; default: printf("Fail to sign up. Maybe this account doesn't exist\n"); break; } continue; case '3': printf("Exit\n", ch); sendline = message_toString(message_construct(SIGNOUT, "0", "0", "0", "0")); if (send(sockfd, sendline, MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) { printf("send() sent a different number of bytes than expected\n"); } if (buffrecv = recv(sockfd, recvline, MESSAGE_MAXLEN, 0) < 0) { printf("str_cli: server terminated prematurely\n"); exit(1); } recvline[strlen(recvline)] = '\0'; puts(recvline); //Debug printf("\nI'm done!\n"); exit(0); break; default: printf("Wrong option! Please choose again\n"); continue; } break; } printf("\n"); }