/*************************************************** * * Empiezo a enviar los datos del archivo * ***************************************************/ // Leo el tamaño del archivo recv(client.fd_client, &file.size_file, sizeof(int), 0); recv(client.fd_client, &file.size_string, sizeof(int), 0); recv(client.fd_client, &file.name_of_file, file.size_string, 0); printf("Tamanio del archivo del cliente: %.3f kB\n", file.size_file/1000.); printf("Recibiendo el archivo \"%s\"...\n", file.name_of_file); if((file.fd_file = creat(file.name_of_file, S_IRWXU)) < 0) { error("No se puede abrir el archivo: "); } while(1) { bzero(buffer, SIZE_BUF); if((numb_bytes = read(client.fd_client, buffer, SIZE_BUF)) < 0) { printf("bytes = %d\n", numb_bytes); error("No se pudo leer el archivo."); } write(file.fd_file, buffer, numb_bytes); recibed += numb_bytes; printf("Recibidos: %.3fkB (%.3f/%.3f) kB\n", numb_bytes/1000., recibed/1000., file.size_file/1000.); if(numb_bytes == 0) break; } //recibed = recv(client.fd_client, file.buffer, file.size_file, 0); printf("Recibidos %.3f/%.3f kB\n", recibed/1000., file.size_file/1000.); //write(file.fd_file, file.buffer, file.size_file);// aux = ntohl(aux);
/*************************************************** * * Empiezo a enviar los datos del archivo * ***************************************************/ // Arreglar con strncpy strcpy(file.name_of_file, argv[3]); file.fd_file = open(file.name_of_file, 0); fstat(file.fd_file, &stat_file); file.size_file = (unsigned int) stat_file.st_size; file.size_string = (unsigned int) strlen(file.name_of_file) + 1; printf("Tamanio del archivo: %.3f kB.\n", file.size_file/1000.); printf("Enviando el archivo \"%s\"...\n", file.name_of_file); //aux = htonl(aux); // Envio los datos del archivo write(conection.sockfd, &file.size_file, sizeof(file.size_file)); write(conection.sockfd, &file.size_string, sizeof(file.size_string)); write(conection.sockfd, &file.name_of_file, sizeof(file.name_of_file)); while(1) {/* num_bytes = read(file.fd_file, &buffer, SIZE_BUF); sended += write(conection.sockfd, &buffer, num_bytes);*/ sended += sendfile(conection.sockfd, file.fd_file, NULL, SIZE_BUF); printf("send: %d\n", sended); printf("num_bytes: %d\n", num_bytes); if(sended == file.size_file) break; } printf("Enviados %.3f/%.3f kB\n", sended/1000., file.size_file/1000.);
Y me surgieron un par de dudas:1. La tranferencia del tamaño y del nombre del archivo se hacen correctamente, lo que no entiendo es por qué cuando uso ntohl me da cualquier cosa. Si no entendí mal, ntohl y ntos se usan antes de transferir números por sockets para lidiar con Little y Big Endian. Por qué entonces me da cualquier cosa?
2. La transferencia del archivo se hace correctamente, pero se transfieren más bytes que los que tiene en realidad el archivo. Probe transferir una imagen pero no se puede abrir así que probé con el source del cliente y llega bien al server (con más bytes) pero cuando lo abro aparece el código del cliente pero al principio del archivo hay basura, lo que me hace suponer lo anterior, que se transfiere bien pero llegan más bytes de los necesarios. Alguien puede darme alguna pista de por qué?