我用libevent2.1.1來編寫一個簡單的http服務器,我想我應該在http_server_callback
中發佈evhttp_request
與evhttp_request_free
。但是當我運行它時,發生錯誤。請告訴我爲什麼,以及我該怎麼做。我應該叫evhttp_request_free釋放http服務器中的資源嗎?
void http_server_callback (struct evhttp_request *req, void *arg)
{
evhttp_send_reply (req, HTTP_OK, "OK", NULL);
evhttp_request_free(req);
}
int http_server_run (void)
{
struct event_base *base;
struct evhttp *http;
struct evhttp_bound_socket *handle;
base = event_base_new();
if (! base)
{
fprintf (stderr, "Couldn't create an event_base: exiting\n");
return 1;
}
http = evhttp_new (base);
if (! http)
{
fprintf (stderr, "couldn't create evhttp. Exiting.\n");
return 1;
}
evhttp_set_gencb (http, http_server_callback, NULL);
handle = evhttp_bind_socket_with_handle (http, "127.0.0.1", 8888);
if (! handle)
{
fprintf (stderr, "couldn't bind to port 8888. Exiting.\n");
return 1;
}
event_base_dispatch (base);
return 0;
}
int main (void)
{
WSADATA WSAData;
WSAStartup (0x101, &WSAData);
http_server_run();
return 0;
}
在此先感謝
顯示完整的碼,錯誤的細節。 – jfly 2014-09-03 04:37:39
謝謝,jfly!我修改了這個問題,併發布完整的代碼和錯誤圖。 – 2014-09-03 07:14:20