当前位置: 软件>C/C++软件
GNU libmicrohttpd
本文导语: GNU libmicrohttpd 是一个小型的嵌入式 HTTP 服务器 的 C 类库,支持 HTTP 1.1 可以同时侦听多个端口,下面是一个最为简单的使用例子: #include #include #include #include #define PAGE "libmicrohttpd demo" "libmicrohttpd demo" static int ahc...
GNU libmicrohttpd 是一个小型的嵌入式 HTTP 服务器 的 C 类库,支持 HTTP 1.1 可以同时侦听多个端口,下面是一个最为简单的使用例子:
#include
#include
#include
#include
#define PAGE "libmicrohttpd demo"
"libmicrohttpd demo"
static int ahc_echo(void * cls,
struct MHD_Connection * connection,
const char * url,
const char * method,
const char * version,
const char * upload_data,
size_t * upload_data_size,
void ** ptr) {
static int dummy;
const char * page = cls;
struct MHD_Response * response;
int ret;
if (0 != strcmp(method, "GET"))
return MHD_NO; /* unexpected method */
if (&dummy != *ptr)
{
/* The first time only the headers are valid,
do not respond in the first round... */
*ptr = &dummy;
return MHD_YES;
}
if (0 != *upload_data_size)
return MHD_NO; /* upload data in a GET!? */
*ptr = NULL; /* clear context pointer */
response = MHD_create_response_from_data(strlen(page),
(void*) page,
MHD_NO,
MHD_NO);
ret = MHD_queue_response(connection,
MHD_HTTP_OK,
response);
MHD_destroy_response(response);
return ret;
}
int main(int argc,
char ** argv) {
struct MHD_Daemon * d;
if (argc != 2) {
printf("%s PORTn",
argv[0]);
return 1;
}
d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
atoi(argv[1]),
NULL,
NULL,
&ahc_echo,
PAGE,
MHD_OPTION_END);
if (d == NULL)
return 1;
(void) getc ();
MHD_stop_daemon(d);
return 0;
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。