int main(int argc, char** argv) { int fd, part_size, file_id; int k = 5; struct file_info* file_info; printf("%10d\n", k); sleep(2); if(argc!=4) { usage(argv[0]); return EXIT_FAILURE; } if(sethandler(SIG_IGN,SIGPIPE)) ERR("Seting SIGPIPE:"); if(sethandler(sigint_handler,SIGINT)) ERR("Seting SIGINT:"); fd = connect_socket(argv[1], atoi(argv[2])); send_message(fd, "UPL", ""); part_size = wait_for_system_readiness(fd); file_info = get_file_info(fd, argv[3], part_size); if(( file_id = send_file_info(fd, argv[3], part_size, file_info)) < 0) ERR("write"); file_info -> file_id = file_id; printf("File ID: %d\n\n\n\\n\n\n\n\n\n\n\\n\n\n\n\n\n\\n\n\n\n\n", file_info -> file_id); send_file(fd, argv[3], file_info, part_size); printf("File ID: %d\n\n\n\\n\n\n\n\n\n\n\\n\n\n\n\n\n\\n\n\n\n\n", file_info -> file_id); if(TEMP_FAILURE_RETRY(close(fd))<0) ERR("close"); free(file_info); return 0; }
int main( int argc, char * argv[] ) { // variables and data structures const char * server_hostname; // (from command line) const char * port_str; // (from command line) char filename[FILENAME_BUF_LEN]; // (from command line) char op_str[3]; int socket_fd; // socket for communicating with server long int file_len; // length of file sent by server unsigned char MD5_hash_server[16]; // array (NOT STRING) holding hex values for MD5 hash from server unsigned char * file_buf = NULL; unsigned char * MD5_hash_client[16]; // POINTER to array (NOT STRING) holding hex values for MD5 hash from client (self) enum OPERATION op = REQ; FILE * file = NULL; struct timeval time_start; struct timeval time_end; struct timeval time_elapsed; // get information from command line analyze_argc(argc, 3, &print_usage); server_hostname = argv[1]; debugprintf("server hostnamename argument: %s", server_hostname); port_str = argv[2]; debugprintf("port argument: %s", port_str); // capture start time if (gettimeofday(&time_start, NULL) == -1) { perror("error getting start time"); exit(EXIT_FAILURE); } debugprintf("start time recorded"); // connect to server socket_fd = connect_to_server(server_hostname, port_str); if (socket_fd == -1) { fprintf(stderr, "failed to connect to server, exiting now\n"); exit(EXIT_FAILURE); } // Receive operation input while(op != XIT){ printf("Please enter an operation:\n"); scanf("%s", &op_str); if(strcmp(op_str, "REQ") == 0){ op = REQ; send_operation(socket_fd, op); // gather file info & send it printf("Please enter the name of the file you would like to request:\n"); scanf("%s", &filename); send_file_info(socket_fd, filename); // receive file length from server long int file_len_net; recv_bytes(socket_fd, &file_len_net, sizeof(file_len_net), "file length"); file_len = file_len_net; debugprintf("file length received from server: %ld", file_len); // quit if file does not exist on server if (file_len == -1) { fprintf(stderr, "File does not exists\n"); close(socket_fd); exit(EXIT_SUCCESS); } // receive MD5 hash from server recv_bytes(socket_fd, MD5_hash_server, 16, "MD5 hash"); debugprintf("MD5 hash received from server"); // prepare to receive file byte array (packet by packet) from server file_buf = (unsigned char *)malloc(file_len * sizeof(unsigned char)); int i_full_pckt; int n_full_pckts = file_len / FILE_PCKT_LEN; size_t last_pckt_len = file_len % FILE_PCKT_LEN; debugprintf("expecting %d full packets from server (%zu bytes)", n_full_pckts, FILE_PCKT_LEN); if (last_pckt_len != 0) { debugprintf("last packet will be %zu bytes", last_pckt_len); } else { debugprintf("no last packet will be received"); } // recieve full packets from server for (i_full_pckt = 0; i_full_pckt < n_full_pckts; i_full_pckt++) { recv_bytes(socket_fd, &file_buf[i_full_pckt * FILE_PCKT_LEN], FILE_PCKT_LEN, "file packet"); debugprintf("full packet %d of %d received from server", (i_full_pckt + 1), n_full_pckts); } // receive last packet from server (if necessary) if (last_pckt_len != 0) { recv_bytes(socket_fd, &file_buf[n_full_pckts * FILE_PCKT_LEN], last_pckt_len, "last file packet"); debugprintf("last packet received from server"); } debugprintf("file received from server"); // create MD5 hash of file MD5_hash_of_byte_array(file_buf, file_len, MD5_hash_client); debugprintf("MD5 hash created"); // compare MD5 hashes if (cmp_MD5_hash(*MD5_hash_client, MD5_hash_server) != 0) { fprintf(stderr, "File hashes do not match – bad transfer\n"); close(socket_fd); exit(EXIT_FAILURE); } debugprintf("MD5 hashes match"); //TODO: MAKE FAIL! // write byte array to file file = fopen(filename, "wb"); size_t write_size = fwrite(file_buf, 1, file_len, file); //return value! debugprintf("file created, DONE %d bytes written", write_size); // capture end time if (gettimeofday(&time_end, NULL) == -1) { perror("error getting end time"); close(socket_fd); exit(EXIT_FAILURE); } debugprintf("end time recorded"); // calculate and print time difference and throughput timersub(&time_end, &time_start, &time_elapsed); double seconds_elapsed = time_elapsed.tv_sec + (time_elapsed.tv_usec / 1000000.0); double throughput = ((double)file_len / 1048576) / seconds_elapsed; printf("%ld bytes transferred in %f sec. Throughput: %f Megabytes/sec. File MD5sum: ", file_len, seconds_elapsed, throughput); print_MD5_hash(MD5_hash_client); printf("\n"); } else if(strcmp(op_str, "UPL") == 0){ op = UPL; send_operation(socket_fd, op); printf("Please enter the name of the file you would like to upload:\n"); } else if(strcmp(op_str, "DEL") == 0){ op = DEL; send_operation(socket_fd, op); // gather file info & send it printf("Please enter the name of the file you would like to delete:\n"); scanf("%s", &filename); send_file_info(socket_fd, filename); // Listen for if file exists uint32_t file_exists_net; recv_bytes(socket_fd, &file_exists_net, sizeof(file_exists_net), "operation"); short int file_exists = ntohs(file_exists_net); if(file_exists){ printf("Are you sure you want to delete %s (Yes/No)?\n", &filename); char confirm_str[3]; scanf("%s", &confirm_str); short int confirm; if( strcmp(confirm_str, "Yes") == 0 ){ confirm = 1; debugprintf("file delete sent to client"); } else { confirm = 0; printf("Delete abandoned by user!\n"); } uint32_t confirm_net; confirm_net = htons(confirm); send_bytes(socket_fd, &confirm_net, sizeof(confirm_net), "confirm delete sent to server"); if( confirm ){ recv_bytes(socket_fd, &confirm_net, sizeof(confirm_net), "confirm delete from server"); confirm = ntohs(confirm_net); if( confirm == 0 ){ printf("File was sucessfully deleted from the server\n"); } else printf("Error deleting file from the server\n"); } } else { debugprintf("Server says file does not exist"); } } else if(strcmp(op_str, "LIS") == 0){ op = LIS; send_operation(socket_fd, op); short int num_files; uint32_t num_files_net; recv_bytes(socket_fd, &num_files_net, sizeof(num_files_net), "client receiving num files"); num_files = ntohs(num_files_net); debugprintf("%d Files in the dir\n", num_files); int i; for(i = 0; i < num_files; i++){ receive_file_info ( socket_fd, filename); printf("%s\n", filename); } } else if(strcmp(op_str, "XIT") == 0){ op = XIT; send_operation(socket_fd, op); } } close(socket_fd); printf("Connection to host closed. Goodbye!\n"); exit(EXIT_SUCCESS); }
//----------------------------------------------------------------------------- void *connection(void *user) { //struct timeval curTime; struct User *_user = (struct User *)user; int e; int rc = 1; int sockfd = _user->sockfd; pthread_detach(pthread_self()); //automatically clears the threads memory on exit fd_set rdset, wrset; int s = -1; int max_fd = sockfd + 1; struct timeval selectTimeout; selectTimeout.tv_sec = TIMEOUT / 1000; selectTimeout.tv_usec = (TIMEOUT % 1000) * 1000; FD_ZERO(&rdset); FD_ZERO(&wrset); char *buf = calloc(100,1); for (;;) { // start monitoring for reads or timeout if (s <= 0) { FD_SET(sockfd, &rdset); } s = select(max_fd, &rdset, &wrset, NULL, &selectTimeout); if (s == -1) { printf("ERROR: Socket error. Exiting.\n"); exit(1); } /* * Here we handle all of the incoming text from a particular client. */ if(s > 0) { bzero(buf,100); rc = recv(sockfd,buf,99,0); if (rc > 0) { char *cmd = malloc(24); cmd = strtok(buf," "); // which command are we processing? if (strcmp(cmd,GET_CMD) == 0) { char *filename = calloc(FILENAME_LEN,1); filename = strtok(NULL,"\0"); send_file_info(sockfd, filename); } else if (strcmp(cmd,LIST_CMD) == 0) { send_file_list(sockfd); } else if (strcmp(cmd,USERS_CMD) == 0) { send_user_list(sockfd); } else if (strcmp(cmd,SEND_LIST_CMD) == 0) { receive_file_list(sockfd,_user); } else if (strcmp(cmd,EXIT_CMD) == 0) { //if I received an 'exit' message from this client pthread_mutex_lock(&mutex); remove_user(_user); pthread_mutex_unlock(&mutex); pthread_exit(NULL); printf("Shouldn't see this!\n"); //loop through global client list and //e =write(..); if (e == -1) //broken pipe.. someone has left the room { pthread_mutex_lock(&mutex); //so remove them from our list pthread_mutex_unlock(&mutex); } } else { printf("Unknown command received: %s\n",cmd); } fflush(stdout); } } } //A requirement for 5273 students: //if I received a new files list from this client, the //server must “Push”/send the new updated hash table to all clients it is connected to. //should probably never get here return 0; }
int client::upload() { char file_name[BUFFSIZE] = {0}; if(m_cmd.size() < 2) { do { cout << "file name: "; cout.flush(); cin.getline(file_name, BUFFSIZE - 1); }while(file_name[0] == '\0'); } else { strncpy(file_name, m_cmd[1], BUFFSIZE); } // Check file's existance in local. if(-1 == access(file_name, F_OK) ) { cout << "no such file" << endl; return -1; } int res = 0; if( -1 == send_file_info(file_name) ) { ;// Send info error. } char pack_type = acceptable(); if( is_pack_type(PT_FEXIST, pack_type) ) { while( 1 ) { char choice[BUFFSIZE] = {0}; cout << "file exist in remote, " << "do you want to cover it ? [yes | no]" << " >> "; cout.flush(); cin.getline(choice, BUFFSIZE - 1); // Default choice is "yes". if('\0' == choice[0]) { strcpy(choice, "yes"); } if( is_no(choice) ) { reply(PT_NOCOVER); return 0; } else if( is_yes(choice) ) { reply(PT_COVER); break; } else { cout << "input error" << endl; } } } res = send_file(file_name); return res; }