项目采用的是libcurl库,实现http客户端功能,将数据内容上传至服务端,并接收应答数据包。libcurl库如果用于http传输json数据、日志上传,使用起来非常便捷,大幅提升研发效率。相比libevent库资源上匹配相差比较大,推荐在简单的http项目中实现数据通讯。
http客户端程序代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#define POSTURL "192.168.101.121" //需要接收消息的IP地址
int http_client_handle(char *url)
{
CURL *curl;
CURLcode res;
struct curl_slist *http_header = NULL;
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if (!curl)
{
fprintf(stderr,"curl init failed\n");
return -1;
}
curl_easy_setopt(curl,CURLOPT_URL,url); //url地址
curl_easy_setopt(curl, CURL