C&;Libevent:将二进制数据添加到输出缓冲区

C&;Libevent:将二进制数据添加到输出缓冲区,c,binary,buffer,libevent,C,Binary,Buffer,Libevent,我有一个输出evbuffer,我想用以下数据填充它: HTTP/1.1 200 OK Date: Tue, 06 Dec 2011 10:35:08 GMT Server: Apache/2.2.14 (Ubuntu) X-Powered-By: PHP/5.3.2-1ubuntu4.9 Vary: Accept-Encoding Content-Encoding: gzip Content-Length: 48 Content-Type: text/html ��(�ͱ���I�O����H

我有一个输出evbuffer,我想用以下数据填充它:

HTTP/1.1 200 OK
Date: Tue, 06 Dec 2011 10:35:08 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.3.2-1ubuntu4.9
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 48
Content-Type: text/html

��(�ͱ���I�O����H�����ч��
                          �4�@�
我使用的是
evbuffer\u add\u printf(…)

我有以下C回调函数:

static void echo_read_cb(struct bufferevent *bev, void *ctx){
    /* This callback is invoked when there is data to read on bev. */
    struct evbuffer *input = bufferevent_get_input(bev);
    struct evbuffer *output = bufferevent_get_output(bev);

    ...
    char* response=NULL;
    response=applyGetReq(url,data,len);

    int contLen=0;
    contLen=getContentLength(response);

    char* binData=strstr(response,"\r\n\r\n");
    binData=binData+strlen("\r\n\r\n");
    fwrite(binData,sizeof(char),contLen,stdout);
    printf("\n");

    evbuffer_add_printf(output,"%s",binData);   //I want to print binData as binary, not printf!!!
}
int
evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen)
{
    size_t need = buf->misalign + buf->off + datlen;
    size_t oldoff = buf->off;

    if (buf->totallen < need) {
            if (evbuffer_expand(buf, datlen) == -1)
                    return (-1);
    }

    memcpy(buf->buffer + buf->off, data, datlen);
    buf->off += datlen;

    if (datlen && buf->cb != NULL)
            (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);

    return (0);
}
所以我有二进制数据指针(binData)和长度(contLen),如何将其打印到输出缓冲区


非常感谢您使用
evbuffer\u add\u printf
无法安全地添加二进制数据。尝试
evbuffer\u添加
功能:

static void echo_read_cb(struct bufferevent *bev, void *ctx){
    /* This callback is invoked when there is data to read on bev. */
    struct evbuffer *input = bufferevent_get_input(bev);
    struct evbuffer *output = bufferevent_get_output(bev);

    ...
    char* response=NULL;
    response=applyGetReq(url,data,len);

    int contLen=0;
    contLen=getContentLength(response);

    char* binData=strstr(response,"\r\n\r\n");
    binData=binData+strlen("\r\n\r\n");
    fwrite(binData,sizeof(char),contLen,stdout);
    printf("\n");

    evbuffer_add_printf(output,"%s",binData);   //I want to print binData as binary, not printf!!!
}
int
evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen)
{
    size_t need = buf->misalign + buf->off + datlen;
    size_t oldoff = buf->off;

    if (buf->totallen < need) {
            if (evbuffer_expand(buf, datlen) == -1)
                    return (-1);
    }

    memcpy(buf->buffer + buf->off, data, datlen);
    buf->off += datlen;

    if (datlen && buf->cb != NULL)
            (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);

    return (0);
}
将数据追加到evbuffer的末尾

参数:


这是
evbuffer\u add
本身的源代码,不是用法示例。对我来说,阅读libevent的源代码比查找文档更容易。是的,起初我以为这是一个片段,但从您的链接中,我看到这是源代码本身。非常感谢。对于文档,您可以尝试libevent.org页面上的链接。有些人觉得这本书很有用;还有最新的强氧剂