Запись памяти в сокет кусками в C

Я пытаюсь записать содержимое памяти в сокет кусками. Я могу записывать файлы, которые меньше моего буфера, но что-нибудь еще, и я в глубокой воде.

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

написанное здесь пишет в мой сокет:

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);
}

Кажется, это должно быть довольно просто, но я изо всех сил пытаюсь найти хорошие примеры.


person Blair    schedule 27.10.2009    source источник


Ответы (2)


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

Вы, конечно, можете переделать цикл, чтобы считать вверх, а не вниз. Я могу думать лучше, считая в обратном порядке.

Изменить: внес несколько изменений на основе комментариев.

person David Oneill    schedule 27.10.2009
comment
Изменил char fileChunk на char *fileChunk, и это сработало. Отлично-большое спасибо. - person Blair; 27.10.2009
comment
Вам действительно не нужна (дорогая) memcopy!. .... writen(ds, fileContents[filesize - sizeRemaining],меньше); - person James Anderson; 27.10.2009
comment
это ведет себя не так, как хотелось бы, если writen() возвращает ‹= 0. - person asveikau; 27.10.2009
comment
@Jason и @asveikau Оба очень хорошие моменты. Я обновил код, чтобы отразить их. - person David Oneill; 27.10.2009
comment
У меня это сработало в конце концов. Полная реализация доступна здесь: github.com/blairyeah/FTP-Client--Server - person Blair; 29.08.2012

Не уверен, почему вы этого хотите, но похоже, что вы хотите что-то вроде:

#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;
    }
}
person asveikau    schedule 27.10.2009