在C中将内存写入块中的套接字

发布于 2024-08-09 04:45:57 字数 1112 浏览 3 评论 0原文

我正在尝试将内存内容分块写入套接字。我可以编写小于我的缓冲区的文件,但其他任何文件都让我陷入困境。

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞花火° 2024-08-16 04:45:57
/* 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
*/

您当然可以修改循环以向上计数而不是向下计数。我可以更好地倒计时。

编辑:根据评论做了一些更改。

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

撩起发的微风 2024-08-16 04:45:57

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

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

Not sure why you want this, but seems like you want something like:

#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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文