int main(int argc, char * argv[]) { int port, sfd; if (!parse_args(argc, argv, &port)) show_help(argc, argv); if (!client_initialize(&sfd, argv[2], port)) { fprintf(stderr, "Failed to initialize the client. Quitting...\n"); exit(1); } if (!client_send_file(sfd, argv[1])) { fprintf(stderr, "Failed to send the file to the server. Quitting...\n"); exit(1); } return 0; }
void client_send_receive_command() { int n; int break_while=0; char r_w_buffer[1024]; int file_flag=0; printf("Please enter the message:"); bzero(r_w_buffer,256); if(strcmp(r_w_buffer,"BYE")==0) exit(1); scanf("%*c%[^\n]",r_w_buffer); n = write(sockfd, r_w_buffer, strlen(r_w_buffer)); char buffer[1024]; strcpy(buffer,r_w_buffer); if (strstr(buffer, "FileDownload") != NULL) { break_while=1; // file_download(); char *pch; char buffer_backup[1024],file_path[1024]; strcpy(buffer_backup, buffer); pch = strtok (buffer_backup, " "); pch = strtok(NULL, " "); pch = strtok (pch, "\n"); strcpy(file_path,CLIENT); strcat(file_path,pch); printf("%s\n",file_path); printf("Waiting for Server to provide Permission\n"); char r_w_buffer[1024]; read(sockfd, r_w_buffer, 1); if(r_w_buffer[0]=='y') { printf("Downloading.. for you\n"); client_receive_file(file_path); } else { printf("Download denied\n"); } } else if (strstr(buffer, "FileUpload") != NULL) { break_while=1; printf("buffer=%s\n",buffer); char *pch; char buffer_backup[1024],file_path[1024]; strcpy(buffer_backup, buffer); pch = strtok (buffer_backup, " "); pch = strtok(NULL, " "); pch = strtok (pch, "\n"); strcpy(file_path,CLIENT); printf("file_path=%s\n",file_path); strcat(file_path,pch); printf("file_path=%s\n",file_path); client_send_file(file_path); } if (n < 0) error("ERROR writing to socket"); bzero(r_w_buffer, 1024); while(1 && !break_while) { bzero(r_w_buffer, 1024); n = read(sockfd, r_w_buffer, 1024); if (n < 0) { break; // error("ERROR reading mach18 from socket"); } //printf("finally\n"); /* if(strcmp(r_w_buffer,"Hi\n") ==0) { file_flag=1; continue; } if(strcmp(r_w_buffer,"Done\n")==0) { file_flag=0; printf("here\n"); continue; } if(file_flag==0) { */ printf("%s ", r_w_buffer); /* } else { FILE *fptr; fptr=fopen("NewDownloaded_file","a"); fprintf(fptr,"%s\n", r_w_buffer); fclose(fptr); } */ if (strstr(r_w_buffer, "---LIST END---\n") != NULL) break; } }
int parse_shell() { char shell_input[MAX_LENGTH]; char message[MAX_LENGTH]; static bool is_client_connected = false; int ret = 0; static int server_sock; /* * Get the input */ fgets(shell_input, MAX_LENGTH, stdin); if(strlen(shell_input) <= 1) return 0; shell_input[strlen(shell_input)-1] = '\0'; strcpy(message, shell_input); /* * Tokenize the strings */ char *arg; int argc = 0; char argv[4][256]; char *temp; char *command = strtok_r(shell_input, " ", &temp); //TODO reorder all commands!! // ----------------Commands common for server/client ------------ if(!strcmp("AUTHOR", command)) { print_success(1, command); LOG("I, g8, have read and understood the course academic integrity policy.\n"); } else if(!strcmp("EXIT", command)) { return 1; } // -------------------Common commands --------------------------- else if(!is_server) { char arg_copy[MAX_LENGTH]; if(temp!=NULL) { strcpy(arg_copy, temp); for(arg = strtok_r(NULL, " ", &temp); arg && argc<4; arg = strtok_r(NULL, " ", &temp)) { strcpy(argv[argc], arg); argc++; } } if(!strcmp("LOGIN", command)) { if(!is_server && !(is_client_connected || argc!=2) && validate_ip(argv[0]) && validate_port(argv[1])) { int newfd = client_connect(argv[0], argv[1]); if(newfd<=1) { print_success(0, command); } else { //TODO client connects to unknown port? is_client_connected = true; add_fd(newfd); client_identify(newfd); server_sock = newfd; print_success(1, command); } } else print_success(0, command); } else if(is_client_connected) { if(!strcmp("PORT", command)) { print_success(1, command); LOG("PORT:%d\n", get_listening_port()); } else if(!strcmp("IP", command)) { print_success(1, command); get_ip(); } else if(!strcmp("SEND", command)) { if(!is_client_connected) print_success(0,command); else print_success(client_send_msg(server_sock, arg_copy), command); } else if(!strcmp("SENDFILE", command)) { if(argc == 2 && validate_ip(argv[0])) { print_success(1, command); client_send_file(argv[0], argv[1]); } else print_success(0, command); } else if(!strcmp("LOGOUT", command)) { if(is_client_connected) { //close(server_sock); //clear_fd(server_sock); client_send(server_sock, command); is_client_connected = false; //server_sock = -1; print_success(1, command); } else print_success(0, command); } else if(!strcmp("LIST", command)) { print_success(1, command); print_client_list(); } else if(!strcmp("REFRESH", command)) { client_send(server_sock, command); print_success(1, command); } else if(!strcmp("BROADCAST", command)) { if(argc) { client_send(server_sock, message); print_success(1, command); } else print_success(0, command); } else if(!strcmp("BLOCK", command)) { if(argc == 1 && verify_ip(argv[0]) && !is_client_blocked(argv[0])) { //TODO CHECK IP in list!! done! print_success(1, command); add_to_block_list(argv[0]); client_send(server_sock, message); } else { print_success(0, command); } } else if(!strcmp("UNBLOCK", command)) { if(argc == 1 && verify_ip(argv[0]) && is_client_blocked(argv[0])) { print_success(1, command); remove_from_block_list(argv[0]); client_send(server_sock, message); } else print_success(0, command); } else print_success(0, command); } else print_success(0, command); } else { if(!strcmp("STATISTICS", command)) { print_success(1, command); print_stats(); } else if(!strcmp("BLOCKED", command)) { if(temp == NULL) print_success(0, command); else { char *ip = strtok_r(NULL, " ", &temp); if(validate_ip(ip) && find_client_by_ip(ip) !=NULL) { print_success(1, command); print_blocked_clients(ip); } else print_success(0, command); } } else if(!strcmp("LIST", command)) { print_success(1, command); print_connected_client_list(); } else if(!strcmp("PORT", command)) { print_success(1, command); LOG("PORT:%d\n", get_listening_port()); } else if(!strcmp("IP", command)) { print_success(1, command); get_ip(); } else print_success(0, command); } LOG("[%s:END]\n", command); return 0; }