Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: send file to socket

I`m trying to send binary file using socket.

        FILE *file;
        char *file_data;
        file = fopen(filepath, "rb");

        //Allocate memory
        file_data=(char *)malloc(fileLen+1);

        //Read file contents into buffer
        fread(file_data, fileLen, 1, file);
        fclose(file);

        sent = send(client, file_data, strlen(header)+fileLen, 0);

It works OK, but some files a too large and I want to read a part to buffer, send it, then read the second part, send it and so on.

I tried to get parts using fread and fgets, but i failed =( How to do it correctly?

UPD: the trouble was in reading incoming request from client. I didnt read it. If i do it, nothing bad happens

like image 556
spe Avatar asked Apr 08 '11 10:04

spe


2 Answers

Something like:

#define CHUNK_SIZE 512

char file_data[CHUNK_SIZE];
//or
char *file_data = malloc(CHUNK_SIZE);

size_t nbytes = 0;
while ( (nbytes = fread(file_data, sizeof(char), CHUNK_SIZE)) > 0)
{
    sent = send(client, file_data, nbytes, 0);
}

But you also need to send the data in chunks, and cannot assume that you'll send all of it in one call.

So that send() would have to look more like this:

    int offset = 0;
    while ((sent = send(client, file_data + offset, nbytes, 0)) > 0) {
            offset += sent;
            nbytes -= sent;
    }

And then you have to be prepared to handle interrupts during the send() call (in which case you'll just need to repeat the send from the same position):

    int offset = 0;
    while ((sent = send(client, file_data + offset, nbytes, 0)) > 0
          || (sent == -1 && errno == EINTR) ) {
            if (sent > 0) {
                offset += sent;
                nbytes -= sent;
            }
    }

In the (hopefully rare) case of EINTR, the send would be repeated on the next loop iteration, and would probably have chance to complete next time and return data to your program

like image 69
Nick Dixon Avatar answered Nov 16 '22 21:11

Nick Dixon


Fread() in chunks

like image 1
Sjoerd Avatar answered Nov 16 '22 23:11

Sjoerd