Search code examples
csocketsstreamchunks

Writing memory to socket in chunks in C


I'm attempting to write memory contents to a socket in chunks. I can write files that are smaller than my buffer, but anything else and I'm in deep water.

/* allocate memory for file contents */
char fileContents = malloc(sizeof(char)*filesize);

/* read a file into memory */
read(fileDescriptor, fileContents , filesize);

int chunksWritten;

/* Write the memory to socket? */
if (filesize > MAX_BLOCK_SIZE){

    while (chunksWritten < filesize){
        // what goes here?
    }

} else {
    chunksWritten = writen(sd, fileContents, filesize);     // this works for files < MAX_BLOCK_SIZE
}

writen here writes to my socket:

int writen(int fd, char *buf, int nbytes) {
    short data_size = nbytes;
    int n, nw;
    if (nbytes > MAX_BLOCK_SIZE)
        return (-3);

    data_size = htons(data_size);
    if (write(fd, (char *) & data_size, 1) != 1) return (-1);
    if (write(fd, (char *) (&data_size) + 1, 1) != 1) return (-1);
    /* send nbytes */
    for (n = 0; n < nbytes; n += nw) {
        if ((nw = write(fd, buf + n, nbytes - n)) <= 0)
            return (nw);
    }
    return (n);
}

This seems like it should be quite easy, but I'm struggling to find any good examples.


Solution

  • /* outside the loop */
    chunksWritten = 0;
    int smaller;
    int r;
    int sizeRemaining = filesize;
    //char *fileChunk = malloc(sizeof(char)*MAX_BLOCK_SIZE+1);
    //memcpy(fileChunk, fileContents, sizeof(char)*MAX_BLOCK_SIZE);
    //r = writen(sd, fileChunk, MAX_BLOCK_SIZE);
    r = writen(sd, fileContents, MAX_BLOCK_SIZE);
    if(r==-1) {
      /* deal with error in a manner that fits the rest of your program */
    }
    chunksWritten = chunksWritten + r;
    sizeRemaining = sizeRemaining - MAX_BLOCK_SIZE;
    
    while(sizeRemaining > 0){
      if(sizeRemaining > MAX_BLOCK_SIZE){
        smaller = MAX_BLOCK_SIZE;
      } else {
        smaller = sizeRemaining;
      }
      //memcpy(fileChunk, fileContents+sizeof(char)*chunksWritten, sizeof(char)*smaller);
      //r = writen(sd, fileChunk, MAX_BLOCK_SIZE);
      r = writen(sd, fileContents[filesize - sizeRemaining], smaller);
      if(r==-1) {
        /* deal with error in a manner that fits the rest of your program */
      }
      sizeRemaining = sizeRemaining - MAX_BLOCK_SIZE;
    }
    
    /*
    Reminder: clean-up fileChunk & fileContents if you don't need them later on
    */
    

    You certainly can rework the loop to count up instead of down. I can think better counting down.

    Edit: made a few changes based on comments.