0

EDIT: Im trying to send a file from the server to the client. When I send a file of 44 bytes, the client reads in 256 bytes on the first iteration (which is the size of char array buf) and on the next iteration reads the remaining 44 bytes. Why does it not read the 44 bytes initially?

Client snippet ( receives file ):

while((bytes_read = read(sd, buf, sizeof(buf))) > 0){ //receving file contents and writing to file
                    printf("DEBUG B: read=%zd\n", bytes_read);
                    fwrite(buf, 1, bytes_read, fp);
                    total_bytes_read += bytes_read;
                printf("DEBUG C: total=%zu\n", total_bytes_read);
                if(ferror(fp)){
                    perror("Error when writing to file\n");
                    exit(1);
                    fclose(fp);
                }
                //printf("Filesize is: %zu \n", filesize);
                if(total_bytes_read == filesize){
                    break;
                }
                }
                printf("The client has received the file\n");
              }

server part(sends file):

strcpy(buf, "no issues");
                if((y = write(sd, buf, sizeof(buf))) < 0){
                    perror("Error reporting back to client\n");
                }
            
            fseek(fp, 0, SEEK_END);
            filesize = ftell(fp);
            fseek(fp, 0, SEEK_SET);
            printf("Sending file size\n");
            
            if((write(sd, &filesize, sizeof(filesize))) < 0){ //sending filesize
                printf("error sending file size\n");
            }
            
            printf("Filesize is: %zu \n", filesize);
            printf("Sending file\n");
            
            while((bytes_read = fread(buf, 1, sizeof(buf), fp)) > 0){ //sending file contents
                printf("DEBUG A: Bytes read %zu \n", bytes_read);
                if ((bytes_written = write(sd, buf, bytes_read)) < 0){
                    printf("Error sending server file.\n");
                }
                printf("DEBUG B: Bytes sent %zu \n", bytes_written);
                total_bytes_sent += bytes_written;
                printf("Total bytes sent %zi \n", total_bytes_sent);
            }
            printf("File has been sent\n");
            fclose(fp);
         }

If more code is required I will post it, let me know. Greatly appreciate any help!

  • Does this answer your question? [c send and receive file](https://stackoverflow.com/questions/11952898/c-send-and-receive-file) –  Nov 04 '20 at 12:23
  • @DavidCullen That was quite helpful but not quite. He says he fixed it by storing the file size into a char array, but that doesnt seem to be my issue. I've just done a littel debugging and for a file of 44 bytes, on the first iteration of writing to the file in the while loop, it writes 256 characters (which is the size of buf) and then proceeds to write 44 bytes before stopping – Darren Fernando Nov 04 '20 at 12:49
  • Are we to interpret your comment just above as a claim that `write(sd, buf, bytes_read)` writes more than `bytes_read` bytes to the file (a socket I guess) associated with file descriptor `sd`? That seems unlikely. But I'm not sure what else to make of it, because surely your debug print statements are telling you the value of `bytes_read`, so you would have described the problem differently if it involved reading more bytes from the source file than you expected. – John Bollinger Nov 04 '20 at 13:14
  • @JohnBollinger No, I apologise if you misunderstood. What's happening is when my client is reading the file contents here this first debug statement `printf("DEBUG B: read=%zd\n", bytes_read)` says it reads 256 bytes, and on the next iteration it reads 44 bytes. I would just like it to read the 44 bytes first – Darren Fernando Nov 04 '20 at 13:18
  • The only plausible possibility I see is that the server sends an additional block of data outside the scope of the server code presented, and previous to it, that the client has not yet read. Note too that in the event that the client has gotten out of sync with the server, it is likely that it has also read the expected file size wrongly, in the sense that the bytes it takes for the file size were really something else. – John Bollinger Nov 04 '20 at 13:23
  • Debug print statement are a tried and true technique, but it should not be the only one in your arsenal. Reducing your code to a [mre] is an excellent debugging technique in its own right, with the added benefit that if it does not help you solve the problem yourself, then the result will help us better help you. Also, learn how to use a debugger. There are also system-specific tools that may sometimes help; on Linux, those include `strace`. – John Bollinger Nov 04 '20 at 13:26

0 Answers0