首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在libevent中读取evbuffer并将其放入字符串(char*)中

如何在libevent中读取evbuffer并将其放入字符串(char*)中
EN

Stack Overflow用户
提问于 2012-08-10 22:30:05
回答 3查看 4.7K关注 0票数 4

我正在使用libevent及其http API编写一个简单的HTTP服务器,该服务器能够编写C servlets。这个servlet可以正常使用GET,但是现在我要用POST发送一些数据,并且我想读取传入的事件缓冲区evb。我想打印/检查evb中存在的数据,但我不能。您知道如何将evb (evbuffer)中的数据放入char*变量中吗?我只看到了操纵缓冲区的方法,但没有读取它。我试过了:

evb->

代码如下:

代码语言:javascript
复制
    #include <stdio.h>
    #include "servlet.h"

    void servlet(struct evhttp_request *req, struct evbuffer *evb) {
        time_t now;
        time(&now);
        evbuffer_add_printf(evb, "<html>\n <head>\n"
            "  <title>%s</title>\n"
            " </head>\n"
            " <body>\n"
            "  <h1>%s</h1>\n"
            " <p>Current time is: %s</p>",
            "C servlet engine", "C servlet", ctime(&now));
        evhttp_add_header(evhttp_request_get_output_headers(req),
            "Content-Type", "text/html");
        evhttp_send_reply(req, 200, "OK", evb);
    }

I am trying this 

void servlet(struct evhttp_request *req, struct evbuffer *evb) {
    size_t len = evbuffer_get_length(evhttp_request_get_input_buffer(req));
    struct evbuffer *in_evb = evhttp_request_get_input_buffer(req);
    char *data;
    evbuffer_copyout(in_evb, data, len);

但我得到总线错误: 10 (我在mac上)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-10 23:00:42

听起来你想用

代码语言:javascript
复制
ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data, size_t datalen)

它将缓冲区内容(最多datalen字节)复制到内存区data。它在libevent book中有记录

票数 3
EN

Stack Overflow用户

发布于 2015-06-04 17:19:15

下面是一个有效的方法:

代码语言:javascript
复制
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);

size_t len = evbuffer_get_length(input);
char *data;
data = malloc(len);
evbuffer_copyout(input, data, len);

printf("we got some data: %s\n", data);

/* Copy all the data from the input buffer to the output buffer. */
evbuffer_add_buffer(output, input);
free(data);
}
票数 2
EN

Stack Overflow用户

发布于 2017-01-25 15:49:19

根据libevent的buffer.h中的源代码,我们应该使用

代码语言:javascript
复制
int evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen);

而不是

代码语言:javascript
复制
ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen);

下面是从buffer.h借用的代码

代码语言:javascript
复制
/**
  Read data from an evbuffer and drain the bytes read.

  If more bytes are requested than are available in the evbuffer, we
  only extract as many bytes as were available.

  @param buf the evbuffer to be read from
  @param data the destination buffer to store the result
  @param datlen the maximum size of the destination buffer
  @return the number of bytes read, or -1 if we can't drain the buffer.
 */
int evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen);

/**
  Read data from an evbuffer, and leave the buffer unchanged.

  If more bytes are requested than are available in the evbuffer, we
  only extract as many bytes as were available.

  @param buf the evbuffer to be read from
  @param data_out the destination buffer to store the result
  @param datlen the maximum size of the destination buffer
  @return the number of bytes read, or -1 if we can't drain the buffer.
 */
ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11903796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档