【问题标题】:Writing memory to socket in chunks in C在C中以块的形式将内存写入套接字
【发布时间】:2009-10-27 03:15:25
【问题描述】:

我正在尝试将内存内容分块写入套接字。我可以写比我的缓冲区小的文件,但其他任何东西我都在深水里。

/* 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 写入我的套接字:

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

这似乎应该很容易,但我正在努力寻找任何好的例子。

【问题讨论】:

    标签: c sockets stream chunks


    【解决方案1】:
    /* 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
    */
    

    您当然可以重新设计循环以向上计数而不是向下计数。我认为倒计时更好。

    编辑:基于 cmets 进行了一些更改。

    【讨论】:

    • 将 char fileChunk 更改为 char *fileChunk,效果很好。完美-非常感谢。
    • 你真的不需要(昂贵的)memcopy!。 .... writen(ds, fileContents[filesize - sizeRemaining],smaller);
    • 如果 writen() 返回
    • @Jason 和@asveikau 两者都很好。我已经更新了代码以反映它们。
    • 我最终得到了这个工作。此处提供完整的实现:github.com/blairyeah/FTP-Client--Server
    【解决方案2】:

    不知道你为什么想要这个,但似乎你想要这样的东西:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 2013-11-14
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      相关资源
      最近更新 更多