将大缓冲区复制到较小缓冲区的块中

发布于 2024-11-08 02:58:16 字数 902 浏览 0 评论 0原文

我有一个已知大小的巨大缓冲区。我需要复制它的块以通过套接字发送到服务器。为此,我尝试一次复制 1024 个字节,然后发送它。

这里的问题是,我不知道缓冲区的内容。它可以是字符串或二进制。它应该适用于缓冲区中包含的所有数据。

这是我编写的代码,当然它不起作用。这只是一个小测试代码,我首先使用一个字符串。稍后我想用一些二进制文件等来测试它。但首先我必须正确理解指针概念!

int main(int argc, char *argv[]){

    char hexstr [] = { 0x02, 0x01, 0x03, 0x04, 0x05, 0x06};
    //trying with a string
    char Bbuf [20]= "Hihelloohowareyou?";
    FillBuffer( Bbuf,sizeof(Bbuf));

        return 0;
}


int FillBuffer(char *bigbuf, int len){

    char smallbuf[4];
    int i = 0;
    int buflen= sizeof(smallbuf);
    printf("length of smallbuf is= %d\n",buflen);
    printf("length of original string= %d\n", len);
    for( i=buflen; i< len+buflen;i =i+buflen){
        memcpy(smallbuf,bigbuf,i);
        printf("i== %d\n", i);
        bigbuf= bigbuf+i;
        //printf("smallbuf conatins= %s\n", smallbuf);
    }

    return 0;
}

提前致谢

I have a huge buffer of known size. I need to copy chunks of it to send via sockets to a server. For this purspose I am trying to copy say 1024 bytes at a time and then send it.

The catch here is, I do not know the contents of the buffer. It could be strings or binary. It should work with all data contained in the buffer.

Here is the code that I have written and that doesnt work of course. It is just a small test code and I use a string first.Later I want to test it with some binary etc. But firstly I have to gt the pointer concepts right!

int main(int argc, char *argv[]){

    char hexstr [] = { 0x02, 0x01, 0x03, 0x04, 0x05, 0x06};
    //trying with a string
    char Bbuf [20]= "Hihelloohowareyou?";
    FillBuffer( Bbuf,sizeof(Bbuf));

        return 0;
}


int FillBuffer(char *bigbuf, int len){

    char smallbuf[4];
    int i = 0;
    int buflen= sizeof(smallbuf);
    printf("length of smallbuf is= %d\n",buflen);
    printf("length of original string= %d\n", len);
    for( i=buflen; i< len+buflen;i =i+buflen){
        memcpy(smallbuf,bigbuf,i);
        printf("i== %d\n", i);
        bigbuf= bigbuf+i;
        //printf("smallbuf conatins= %s\n", smallbuf);
    }

    return 0;
}

Thanks in advance

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

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

发布评论

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

评论(4

暮年慕年 2024-11-15 02:58:16

我不确定你在那里做什么,但我猜你正在尝试做类似的事情:

void Stuff(char* bbuf, int size)
{
    char buf[4];
    int remain = size;
    while (remain)
    {
      int toCpy = remain > sizeof(buf) ? sizeof(buf) : remain;
      memcpy(buf, bbuf, toCpy);
      bbuf += toCpy;
      remain -= toCpy;
      DoStuff(&buf, toCpy); //Send the buffer or whatever
    }
}  

Im not sure what you are doing there, but I guess you are trying to do something like:

void Stuff(char* bbuf, int size)
{
    char buf[4];
    int remain = size;
    while (remain)
    {
      int toCpy = remain > sizeof(buf) ? sizeof(buf) : remain;
      memcpy(buf, bbuf, toCpy);
      bbuf += toCpy;
      remain -= toCpy;
      DoStuff(&buf, toCpy); //Send the buffer or whatever
    }
}  
魂归处 2024-11-15 02:58:16

您的代码基本上是正确的。唯一不起作用的部分是 printf,因为如果您有二进制内容,则无法在控制台中显示它。

另请注意,当发送到套接字(非结束接收器)时,您必须指定要发送的内容的长度,以便另一端知道要读取多少字节。

Your code is essentially right. The only part that wouldn't work is the printf, because if you have binary content, you can't show it in the console.

Also, note that when sending to sockets, that are non-ending sinks, you have to specify the length of what you're sending, for the other end to know how many bytes to read.

故乡的云 2024-11-15 02:58:16

您的循环应该是这样的:

for(i = 0; i < len; i += buflen)
{
    memcpy(smallbuf,bigbuf+i,buflen);
    printf("i== %d\n", i);
    // Note that printf wont work since the string is not NULL-terminated.
    //printf("smallbuf conatins= %s\n", smallbuf);
}

如果您想通过网络发送此数据,您可能希望将缓冲区放入数据包结构中,至少包含有关有多少数据实际上有用的信息(如果大缓冲区大小不可整除)由小一个)。如果您想发送字符串以外的其他数据,则不能真正使用 NULL 终止。

Your loop should be like this:

for(i = 0; i < len; i += buflen)
{
    memcpy(smallbuf,bigbuf+i,buflen);
    printf("i== %d\n", i);
    // Note that printf wont work since the string is not NULL-terminated.
    //printf("smallbuf conatins= %s\n", smallbuf);
}

If you want to send this over network, you would probably want to put you buffer into a packet struct with at least information on how much of the data is actually useful (if the big buffer size is not divisible by the small one). If you want to send other data than strings, you can't really use NULL-termination for this.

终难愈 2024-11-15 02:58:16

无需复制任何内容,因为 sendto 和 write 都采用大小参数。如果缓冲区 B 中有 20 字节的数据,并且希望在套接字上将其分成两个大小为 10 的数据包,则只需执行以下操作:(

write( sock, B, 10 );
write( sock, B + 10, 10 );

或 s/write/sendto/ 并添加一个 flags 参数)。

在这种情况下使用 memcpy 只是浪费时间。

There is no need to copy anything, as both sendto and write take a size argument. If you have 20 bytes of data in the buffer B, and you want to break it into two packets of size 10 on the socket, you just do:

write( sock, B, 10 );
write( sock, B + 10, 10 );

(or s/write/sendto/ and add a flags argument).

Using memcpy is just wasting time in this situation.

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