メモリの内容をソケットにチャンクで書き込もうとしています。自分のバッファよりも小さいファイルを書くことはできますが、それ以外は何でも、私は深海にいます。
/* 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);
}
これは非常に簡単なはずですが、良い例を見つけるのに苦労しています。