0

I'm programming a server with C and I send file with my client. I receive the file with my server and i want to write it on disk.
I print it to be sure i had it.
Everything are ok but when i write on disk, it's doesn't write everything.
This is a part of my server code :

.
.
.
#define MAX 1024
void whileRead(int *sockfd)
{
    FILE *output = fopen("copy", "w");
    int n;
    char *buff = malloc(sizeof(char) * MAX);

    while(1) {
        bzero(buff, sizeof(buff));
        //if something in the buffer then print it  
        if(read(*sockfd, buff, sizeof(buff)) > 0){
            printf("%s", buff);
            fprintf(output, "%s", buff) // try too with fputs(buff,output)
            if ((strncmp(buff, "exit", 4)) == 0) {
                printf("Client Exit...\n");
                break;
            }
        }
    }
    free(buff);
    fclose(output);
    pthread_exit(NULL);
}
.
.
.

My client :

.
.
.
#define SIZE 1024
struct arg_struct {
        SOCKET sock;
        FILE* input;
    };


void *whileWrite(void* arg)
{
    int n;
    char data[SIZE] = {0};
    struct arg_struct *args = (struct arg_struct*)arg;
    while(fgets(data, SIZE, args->input) != NULL) {
        if (send(args->sock, data, sizeof(data), 0) < 0) {
            perror("[-]Error in sending file.");
            exit(1);
        }
        bzero(data, SIZE);
    }
}
.
.
.

Do i need to have a bigger buffer ? Or it's another problem ?
Thank you !

  • 1
    Likely not the root cause, but you are not verifying the return values from either your call to `fopen` or `malloc` in the server code. Should _always_ check return values of functions designed to return them. And here `bzero(buff, sizeof(buff));` `sizeof(buff)` is only the size of the pointer, not which is probably not what you intend. – ryyker Oct 11 '22 at 11:55
  • `if(read(*sockfd, buff, sizeof(buff)) > 0)` is going to read only 4 (or maybe 8) bytes of data. `if(read(*sockfd, buff, MAX) > 0)` will read `MAX` bytes. – ryyker Oct 11 '22 at 12:00
  • but i did `char *buff = malloc(sizeof(char) * MAX);` So sizeof(buff) as the same size than MAX no ? – Amusing6028 Oct 11 '22 at 12:13
  • In any case, your code cannot handle general binary fikes that may contain embedded nulls. – Martin James Oct 11 '22 at 12:15
  • Place this line in your code under where `buff` is created to test the size in bytes: `size_t size = sizeof(buff);` and see what you get. – ryyker Oct 11 '22 at 12:15
  • In TCP socket code, it's much safer to avoid str* calls, and the like, if you can. The results returned from calls like read() are rhe ONLY way to be sure how much data has been loaded into/outof buffers. – Martin James Oct 11 '22 at 12:23
  • So, you do not want, or need, bzero, str*, sprintf or anything like that which relies on, or tries to enfirce, NUL-terminationin buffers. – Martin James Oct 11 '22 at 12:29
  • ' if ((strncmp(buff, "exit", 4)) == 0' is unreliable. Each of the five chars may be received by five read calls loading one byte each time. An extreme example yes, but it could legally happen with TCP byte streaming. – Martin James Oct 11 '22 at 12:34
  • @Amusing6028 No. If you did `char *buff = malloc...`, sizeof (buff) is still the size of a `char *`. It will always be the size of a `char *`, because `buff` will always be a `char *`. – William Pursell Oct 11 '22 at 12:49

0 Answers0