c++ libCurl : Post 发送多于一个字节的回调函数

c++ libCurl : Post callback function to send more than one byte

我正在使用 Libcurl post 回调从文件发送数据。示例 here 显示了如何从回调函数每次调用发送 1 个字节的数据。 我已经更改了代码,因此文件被读入块。这几乎可以正常工作。

当前示例代码为:

if(sizeleft){
 *( char *)ptr = readptr[0]; 
   readptr++;
   sizeleft--;
  return 1;
}

This example sends the data as 1 byte. but suppose i have to send it multiple bytes.I have tried to increment readptr by two each time and decreasing sizeleft by two and i return 2bytes at a time.

它不是这样工作的,数据已损坏。

如果有人能帮助我,我将不胜感激。 谢谢

很难从你的问题中准确判断出你在做什么、你期望发生什么以及实际发生了什么。不过看起来您的方向是对的。

CURLOPT_READFUNCTION 的 documentation 指出 size * nitems(示例中的 a.k.a。size * nmemb)是 上限 您可以写入 buffer 的字节数,而您的函数的 return 值是您写入的 实际 字节数。返回零意味着你已经写完了你想写的所有内容,你的回调函数将不再被调用。

如果您从函数中 return 得到的值不等于您实际写入缓冲区的字节数,则可能会损坏。

PS:类似于:

// copy as many bytes as we can, up to either:
// * The number of bytes we have remaining.
//   or
// * The space available in ptr.
size_t maxBytes = size * nmemb;
size_t numBytes = std::min (maxBytes, sizeleft);
memcpy (ptr, readptr, numBytes);
readptr += numBytes;
sizeleft -= numBytes;
return numBytes;