list file and send over a socket C -
this snippet of "list" function of ftp server variabels use. school project , has one requirement: it has work. no matter if code looks bad...this 5th program (and harder i've ever done) xd
here's problem:
/* here variabels */ int sockd, newsockd, socket_len, rc, rc_list, fd, fpl, i; int numporta = atoi(argv[1]); struct sockaddr_in serv_addr, cli_addr; off_t offset = 0, offset_list = 0; struct stat stat_buf; struct hostent *local_ip; static char filename[1024], buffer[256], saved_user[256]; char *user_string = null, *username = null, *pass_string = null, *password = null, *other = null; size_t fsize; char **files; file *fp_list; size_t count; /* , here snippet of list */ if(recv(newsockd, buffer, sizeof(buffer), 0) < 0){ perror("errore nella ricezione del nome utente"); onexit(newsockd, sockd, 0, 2); } other = strtok(buffer, "\n"); if(strcmp(other, "list") == 0){ printf("received list request\n"); } else onexit(newsockd, sockd, 0, 2); count = file_list("./", &files); return 0; if((fp_list = fopen("listfiles.txt", "w")) == null){ perror("impossibile aprire il file per la scrittura list"); exit(exit_failure); } for(i=0; < count; i++){ if(strcmp(files[i], "dir ..") == 0 || strcmp(files[i], "dir .") == 0){ continue; /* if dir . or .. skip */ } else{ fprintf(fp_list, "%s\n", files[i]); } } if((fpl = open("listfiles.txt", o_rdonly)) < 0){ perror("open file open"); exit(1); /*sendfile , fstat need int double file opening :( */ } if(fstat(fpl, &stat_buf) < 0){ perror("errore fstat"); onexit(newsockd, sockd, 0, 2); } fsize = stat_buf.st_size; if(send(newsockd, &fsize, sizeof(fsize), 0) < 0){ perror("errore durante l'invio grande file list"); onexit(newsockd, sockd, 0, 2); } rc_list = sendfile(newsockd, fpl, &offset_list, stat_buf.st_size); if(rc_list == -1){ perror("invio file list non riuscito"); close(newsockd); close(sockd); fclose(fp_list); } if(rc_list != fsize){ fprintf(stderr, "trasferimento incompleto: %d di %d bytes\n", rc_list, (int)stat_buf.st_size); close(newsockd); close(sockd); fclose(fp_list); } fclose(fp_list); close(fpl); fsize = 0; /* fsize 0 because have reuse */ printf("file inviato\n"); memset(buffer, '0', sizeof(buffer)); /* clear buffer future use */ where function file_list is:
size_t file_list(char *path, char ***ls){ dir *dp; struct stat filestat; struct dirent *ep = null; size_t len, count = 0; int file = 0; *ls = null; dp = opendir (path); if(dp == null){ fprintf(stderr, "non esiste la directory: %s\n", path); exit(1); } ep = readdir(dp); while(null != ep){ count++; ep = readdir(dp); } rewinddir(dp); *ls = calloc(count, sizeof(char *)); count = 0; ep = readdir(dp); while(null != ep){ if((file = open(ep->d_name, o_rdonly)) < 0){ perror("apertura file"); exit(1); } if(fstat(file, &filestat) != 0){ perror("filestat"); free(*ls); exit(exit_failure); } if(s_isdir(filestat.st_mode)){ len = strlen(ep->d_name); (*ls)[count] = malloc(len+5); /* lunghezza stringa + "dir \n" */ strcpy((*ls)[count], "dir "); /* copio dir */ strcat((*ls)[count++], ep->d_name); free((*ls)[count]); ep = readdir(dp); } else{ (*ls)[count++] = strdup(ep->d_name); ep = readdir(dp); } (void)closedir(dp); return count; } } so program when client send "list" ther server?
server create file called listfiles.txt , write files , directories have been found "./"
next sends file client.
problem file listed files sent without nothing or not list (like program name, or 3 letters, etc).
not client problem because i've stopped server before sent files , result have described above :(
Comments
Post a Comment