Question

J'essaie d'écrire le contenu de la mémoire sur un socket en morceaux. Je peux écrire des fichiers plus petits que mon tampon, mais n’importe quoi d’autre et je suis en eau profonde.

/* 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
}

écrit ici écrit dans mon 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);
}

Cela semble être une tâche facile, mais j'ai du mal à trouver de bons exemples.

Était-ce utile?

La 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
*/

Vous pouvez certainement retravailler la boucle pour compter plus haut que bas. Je peux penser à mieux compter.

Modifier: quelques modifications ont été apportées en fonction des commentaires.

Autres conseils

Vous ne savez pas pourquoi vous voulez cela, mais vous semblez vouloir quelque chose comme:

#define MIN(x, y) ((x) < (y) ? (x) : (y))

while (chunksWritten < filesize) {
    int writtenThisPass = writen(fd,
                                 fileContents + chunksWritten,
                                 MIN(filesize - chunksWritten, MAX_BLOCK_SIZE));

    if (writtenThisPass <= 0)
    {
        // TODO: handle the error
    }
    else
    {
        chunksWritten += writtenThisPass;
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top