Amusing6028
Amusing6028

Reputation: 1

How to copy my file on disk after receive from a socket in C?

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 !

Upvotes: 0

Views: 52

Answers (0)

Related Questions