1

我创建了简单的基于 evhttp 的服务器。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <event.h>
#include <evhttp.h>

void
handler(struct evhttp_request *req, void *arg) {
  struct evbuffer *buf;
  buf = evbuffer_new();

  if(buf == NULL) {
    fprintf(stderr, "ERROR: Failed to create response buffer\n");
    exit(EXIT_FAILURE);
  }

  evbuffer_add_printf(buf, "Server called");
  evhttp_send_reply(req, HTTP_OK, "OK", buf);
}

int
main(int argc, char **argv) {
  struct evhttp *http;

  event_init();
  http = evhttp_start("0.0.0.0", 8081);

  evhttp_set_gencb(http, handler, NULL);

  event_dispatch();
  evhttp_free(http);

  exit(EXIT_SUCCESS);
}

当我开始使用它进行基准测试时

ab -r -n 1000 -c 50 http://0.0.0.0:8081/

经过几次尝试后,我收到了这些警告:

[warn] Error from accept() call: Too many open files

有点像我没有关闭套接字......并发级别 50 的目标是一次只使用 50 个套接字,对吧?

我应该在处理函数中关闭套接字吗?

4

2 回答 2

1

打开文件描述符的 ulimit 设置是什么?例如。尝试执行 'ulimit -n' 命令(不带引号)。这些是允许在您的应用程序中打开的文件描述符的数量。要增加你可以使用 ulimit 命令。例如 ulimit -n 10240

于 2012-03-16T10:17:00.843 回答
0

我开始使用event2/*库并添加evbuffer_free(buf)到处理函数的末尾,它完成了这项工作。

于 2012-02-28T15:09:25.740 回答