Reputation: 1779
This is a snippet of client.c:
size_t fsize;
int total_bytes_read = 0, ret_val;
hp = gethostbyname(argv[1]);
DescrittoreClient = socket(AF_INET, SOCK_STREAM, 0);
connect(DescrittoreClient, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
strcpy(Buffer, filename);
send(DescrittoreClient, Buffer, strlen(Buffer), 0);
ret_val = read(DescrittoreClient, &fsize, sizeof(fsize));
if(ret_val == -1){
printf("Errore durante ricezione grandezza file\n");
close(DescrittoreClient);
exit(1);
}
fd = open(filename, O_CREAT | O_WRONLY,0644);
if (fd < 0) {
perror("open");
exit(1);
}
while(total_bytes_read < fsize){
while ((nread = read(DescrittoreClient, Buffer, sizeof(Buffer))) > 0){
write(fd, Buffer, nread);
total_bytes_read += nread;
}
}
printf("File ricevuto\n");
And this is a snippet of server.c:
int DescrittoreServer, DescrittoreClient, LunghezzaClient;
int NumPorta = atoi(argv[1]);
char Buffer[1024] = {};
int rc, fd;
off_t offset = 0;
struct stat stat_buf;
char filename[1024] = {};
int fsize[10240] = {};
DescrittoreServer = socket(AF_INET, SOCK_STREAM, 0);
if(bind(DescrittoreServer, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
perror("Errore di bind\n");
close(DescrittoreServer);
exit(1);
}
listen(DescrittoreServer, 5);
LunghezzaClient = sizeof(cli_addr);
while(1){
DescrittoreClient = accept(DescrittoreServer, (struct sockaddr *) &cli_addr, &LunghezzaClient);
rc = recv(DescrittoreClient, filename, sizeof(filename), 0);
fprintf(stderr, "Ricevuta richiesta di inviare il file: '%s'\n", filename);
/* open the file to be sent */
fd = open(filename, O_RDONLY);
/* get the size of the file to be sent */
fstat(fd, &stat_buf);
*fsize = stat_buf.st_size;
send(DescrittoreClient, fsize, sizeof(fsize), 0);
/* copy file using sendfile */
offset = 0;
rc = sendfile(DescrittoreClient, fd, &offset, stat_buf.st_size);
if (rc != stat_buf.st_size) {
fprintf(stderr, "Trasferimento incompleto: %d di %d bytes\n", rc, (int)stat_buf.st_size);
exit(1);
}
close(DescrittoreClient);
close(fd);
}
What does this code do:
1) client sends the name of a file
2) server sends the length (in byte) of the file
3) client receives the length of the file
4) server sends the file
5) client receives the file
The problem is that the file is not entirely received but only a part of it is written by the client (for example a file of 2143 byte is sent by the server and only 95 byte are received by the client) and i don't understand why!
PS: some errors handling have been deleted so the code is more readable!
Upvotes: 1
Views: 5642
Reputation: 1779
The problem is that i have declared char nread=0
instead of int nread=0
into the client and so the maximum value i can read with char
is 127...changed char to int solved my problem!
Upvotes: 0
Reputation: 2707
You have to call recv() in a loop and sum up it's return values:
while (read_bytes < filesize) {
read_bytes += recv(socket, buffer + read_bytes, filesize-read_bytes, flags)
}
of course you should also add error handling, i.e., check if you read 0 bytes and the connection was closed.
Upvotes: 2