void bad_exit(){ close(sockfd); free(command); free(channel); free(message); cooked_mode(); exit(1); }
void clean_exit(){ close(sockfd); free(command); free(channel); free(message); cooked_mode(); exit(0); }
// Processes the input string to decide what type of command it is. bool ProcessInput(std::string input) { std::vector<std::string> inputs = SplitString(input); if (inputs[0] == "/exit") { SendLogout(); cooked_mode(); return false; } else if (inputs[0] == "/list") { SendList(); } else if (inputs[0] == "/join" && inputs.size() > 1) { SendJoin(inputs[1]); } else if (inputs[0] == "/leave" && inputs.size() > 1) { SendLeave(inputs[1]); } else if (inputs[0] == "/who" && inputs.size() > 1) { SendWho(inputs[1]); } else if (inputs[0] == "/switch" && inputs.size() > 1) { SwitchChannel(inputs[1]); } else { std::cout << "*Unknown command" << std::endl; } PrintPrompt(); return true; }
int main(int argc, char *argv[]) { /* handle arguments */ if(argc != 4) { printf("Incorrect arguments, please use:\n./client server port username\n"); return 1; } raw_mode(); memset(liveChannel, '\0', CHANNEL_MAX); char * address = argv[1]; char * port = argv[2]; char * username = argv[3]; /* Build Socket we pass the function the address and port */ if(buildSocket(address, port) != true) { cooked_mode(); return 1; } /* Handle Login */ if(login(username) != true) { cooked_mode(); return 1; } int parseStatus = true; char * input; fd_set readfds; promptUser(); do { FD_ZERO(&readfds); FD_SET(0, &readfds); FD_SET(socketfd, &readfds); select(socketfd+1, &readfds, NULL, NULL, NULL); if(FD_ISSET(0, &readfds)) { input = inputString(); if(input != NULL) { /* Parse Input */ parseStatus = parseInput(input); if(parseStatus != -1) { promptUser(); } } } else if (FD_ISSET(socketfd, &readfds)) { struct text * text = (struct text *) malloc(sizeof(struct text) + 1024); int bytes = 0; if((bytes = recvfrom(socketfd, text, 1024, 0,servinfo->ai_addr, &servinfo->ai_addrlen)) > 0) { clearPrompt(); switchResp(text); free(text); } } } while(parseStatus != -1); freeaddrinfo(servinfo); cooked_mode(); return 0; }