代码:
h文件
- #include
- #include
-
- #include
- #include
- #include
- #include
-
- #include
- #include
- #include
- #include
- #include
-
- #include
-
- #define ETH_NAME "eth0"
- #define MYHTTPD_SIGNATURE "MoCarHttpd v0.1"
-
- using namespace std;
-
- class RouteTasks{
- public:
- RouteTasks();
-
- void init();
-
- private:
- static void routeTasks(struct evhttp_request *req, void *arg);
-
- string getIp();
-
- int m_port;
- int m_timeout;
- };
cpp文件
- #include "platfromcommunication.h"
-
- RouteTasks::RouteTasks()
- {
- m_port = 7777;
- m_timeout = 120;
- }
-
- void RouteTasks::init()
- {
- //初始化event API
- event_init();
-
- //创建一个http server
- struct evhttp *httpd;
-
- httpd = evhttp_start(getIp().c_str(), m_port);
-
- evhttp_set_timeout(httpd, m_timeout);
-
- //也可以为特定的URI指定callback
- evhttp_set_cb(httpd, "/wayLine/start", &RouteTasks::routeTasks, NULL);
-
- //循环处理events
- event_dispatch();
-
- evhttp_free(httpd);
- }
-
- void RouteTasks::routeTasks(evhttp_request *req, void *arg)
- {
- char request_data[4096] = {0};
-
- //获取POST方法的数据
- size_t post_size = EVBUFFER_LENGTH(req->input_buffer);
- char *post_data = (char *) EVBUFFER_DATA(req->input_buffer);
- memcpy(request_data, post_data, post_size);
- printf("got startRouteTasks connection post_data = [%s], len =%ld\n", post_data, post_size);
-
- //解析获得的数据
- string task_code;
- string stream;
-
- Json::Value jvalue;
- Json::Reader jreader;
- jreader.parse(request_data, jvalue);
- task_code = jvalue["task_code"].asString();
- stream = jvalue["stream"].asString();
- cout<<"task_code: "<<task_code<<" stream: "<<stream<<"\n-------------------------"<<endl;
-
- //给前段回复一个响应结果
- Json::Value root;
- Json::FastWriter fastWriter;
- root["code"] = Json::Value("0");
- root["msg"] = Json::Value("成功");
- cout<<"return post"<<fastWriter.write(root)<<endl;
-
- /* 输出到客户端 */
-
- //HTTP header
- evhttp_add_header(req->output_headers, "Server", MYHTTPD_SIGNATURE);
- evhttp_add_header(req->output_headers, "Content-Type", "application/json; charset=UTF-8");
- evhttp_add_header(req->output_headers, "Connection", "close");
-
- //输出的内容
- struct evbuffer *buf;
- buf = evbuffer_new();
- evbuffer_add_printf(buf, "%s", fastWriter.write(root).c_str());
-
- //将封装好的evbuffer 发送给客户端
- evhttp_send_reply(req, HTTP_OK, "OK", buf);
-
- evbuffer_free(buf);
-
- }
-
- string RouteTasks::getIp()
- {
- struct sockaddr_in sin;
- struct ifreq ifr;
-
- int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- if(sockfd == -1) {
- perror("socket error");
- return "";
- }
-
- strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ);
-
- if(ioctl(sockfd, SIOCGIFADDR, &ifr) == 0) {
- memcpy(&sin, &ifr.ifr_addr, sizeof (ifr.ifr_addr));
- cout<<ETH_NAME<<" ip : "<<inet_ntoa(sin.sin_addr)<<endl;
- return inet_ntoa(sin.sin_addr);
- }
-
- return "";
- }
main文件
- #include
- #include
- #include
- #include
- #include
-
- #include "...h"
-
- using namespace std;
-
- //当向进程发出SIGTERM/SIGHUP/SIGINT/SIGQUIT的时候,终止event的事件侦听循环
- void signal_handler(int sig) {
- switch (sig) {
- case SIGTERM:
- case SIGHUP:
- case SIGQUIT:
- case SIGINT:
- event_loopbreak(); //终止侦听event_dispatch()的事件侦听循环,执行之后的代码
- break;
- }
- }
-
- int main()
- {
- cout << "Hello World!" << endl;
-
- signal(SIGHUP, signal_handler);
- signal(SIGTERM, signal_handler);
- signal(SIGINT, signal_handler);
- signal(SIGQUIT, signal_handler);
-
-
- RouteTasks rt;
-
- rt.init();
-
- while (1) {
- sleep(1);
- }
-
- return 0;
- }
http post与get请求:c++ curl post和get请求返回数据解析_c++解析curl返回的json_小鱼仙官的博客-CSDN博客