一、需要安装mongoose库(可在github上下载)
主要是需要用到以下两个文件
源码如下:
http_server.cpp:
#include "mongoose.h"
#define WWWROOT "./wwwroot/"
void cb(struct mg_connection *c,int ev,void *ev_data,void *fn_data)
{
struct mg_http_serve_opts opts = {.root_dir = WWWROOT};
if(ev == MG_EV_HTTP_MSG)
{
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
if(mg_http_match_uri(hm,"/hi"))
{
mg_http_reply(c,200,"Content-Type:text/html\r\n","<html><body><h1>%s</h1?</body></html>","hello word");
}
else
{
mg_http_serve_dir(c,hm,&opts);
}
}
return;
}
int main()
{
struct mg_mgr mgr;
struct mg_connection *c;
mg_mgr_init(&mgr);
c=mg_http_listen(&mgr,"0.0.0.0:19000",cb,&mgr);
while(1)
{
mg_mgr_poll(&mgr,1000);
}
mg_mgr_free(&mgr);
return 0;
}
makefile:
http_server:http_server.cpp mongoose.c
g++ -std=c++11 $^ -o $@
文件结构:
在浏览器中输入192.168.164.128:19000/hi ---随自己情况而定
结果如下: