最近在学习网络编程相关知识,遂有想法做点小东西来练手,大致框架为,TX2板子搭载深度相机获取图像信息,本地运行ORB_SLAM解算位姿信息,传输OPENCV_MAT格式图像信息至服务器运行yolo3识别目标物体,返回像素坐标,对下一步小车运行轨迹进行导航规划。
现在搭建HTTP协议框架实现传输图像信息至服务器功能
1.首先下载现有的HTTP轮子Mongoose,并回退至5.1版本(函数接口不同)
git clone https://github.com/cesanta/mongoose.git mongoose
cd mongoose
git checkout fa351959a0
正常应返回如下结果
Note: checking out 'fa351959a0'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b
HEAD 目前位于 fa35195... 5.0 -> 5.1
2.新建工程文件,将mongoose中的mongoose.c与mongoose.h添加至工程文件中。
本人测试工程文件目录如下:
其中test.cpp文件为
#include
#include
#include
#include
#include "mongoose.h"
using namespace std;
int env_handler(struct mg_connection *conn);
int main(int argc, char *argv[])
{
struct mg_server* server;
server = mg_create_server(NULL); // 初始化一个mongoose server
mg_set_option(server, "listening_port", "8003"); // 设置端口号为8003
mg_add_uri_handler(server, "/", env_handler); // 设置回调函数
printf("Starting on port %s ...\n", mg_get_option(server, "listening_port"));
while (1) {
mg_poll_server(server, 100); // 超时时间(ms)
}
mg_destroy_server(&server);
return 0;
}
int env_handler(struct mg_connection *conn)
{
static int counter = 0;
counter++;
printf("counter: %d\n", counter);
const char * str_data = conn->content; // 服务端收到的消息
int str_len = conn->content_len; // 服务端收到的消息长度
string str(str_data, str_len);
mg_printf(conn, "Received: %s, %d", str.c_str(), counter);
return 0;
}
3.编写CMakeLists.txt文件,添加monogoose至编译链接
本人CMakeLists.txt文件如下:
cmake_minimum_required( VERSION 2.8 )
project( http_conn )
set( CMAKE_BUILD_TYPE Release )
set( CMAKE_CXX_FLAGS "-std=c++11 -O3" )
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
# opencv
find_package( OpenCV REQUIRED )
find_package( Threads )
include_directories(
${OpenCV_INCLUDE_DIRS}
)
include_directories( ${PROJECT_SOURCE_DIR}/include )
add_executable( http_conn src/mongoose.c src/test.cpp )
target_link_libraries( http_conn
${OpenCV_LIBS}
${CMAKE_THREAD_LIBS_INIT}
)
本处坑为,monogoose中对
CMakeFiles/http_conn.dir/src/mongoose.c.o:在函数‘mg_start_thread’中:
mongoose.c:(.text+0x32df):对‘pthread_create’未定义的引用
collect2: error: ld returned 1 exit status
CMakeFiles/http_conn.dir/build.make:165: recipe for target 'http_conn' failed
make[2]: *** [http_conn] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/http_conn.dir/all' failed
make[1]: *** [CMakeFiles/http_conn.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
因此,我们需要声明 find_package(Threads) 以及target_link_libraries( ${CMAKE_THREAD_LIBS_INIT} )。
1.libcurl的安装与配置
安装依赖项
sudo apt-get install autoconf
sudo apt-get install automake
sudo apt-get install libtool
在github上下载curl源码:https://github.com/curl/curl
cd curl
./buildconf
./configure
make
sudo make install
新建工程文件目录如下:
其中test.cpp内容
#include
#include
#include
#include
#include "http.h"
using namespace std;
int env_handler(struct mg_connection *conn);
int main(int argc, char *argv[])
{
CurlHttp curl_http;
string str_url = "http://202.55.666.77:8003";
string str_test = "Hello World!";
while(1)
{
cout << "Post data ...\n";
string result;
int res = curl_http.http_post(str_url.c_str(),str_test.c_str(),&result);
cout << "[Response]:" << result << '\n';
sleep(1);
}
return 0;
}
此处string str_url = "http://202.55.666.77:8003"; 其中端口号8003为自己指定,而前面的IP地址则为Sever服务器端的IP地址,此处坑为,本实验室的服务器是连接于路由器之上,因此我的主机同服务器之间通过IP地址链接还需要路由器在中间设置一次地址映射,否则一直处于丢失状态。
http.h内容
#include
#include
#include
#include
#include
#include
using namespace std;
class CurlHttp
{
public:
CurlHttp()
{
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
}
~CurlHttp()
{
curl_easy_cleanup(curl);
}
static int write_data(void* ptr,size_t size,size_t nmemb, string& data)
{
data = string((char* )ptr);
return 0;
}
int http_post(const char* url, const char* post_data, string* result)
{
curl_easy_setopt(curl,CURLOPT_URL,url);
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,post_data);
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,result);
return curl_easy_perform(curl);
}
private:
CURL* curl;
};
此工程CmakeLists.txt为
cmake_minimum_required( VERSION 2.8 )
project( http_client )
set( CMAKE_BUILD_TYPE Release )
set( CMAKE_CXX_FLAGS "-std=c++11 -O3" )
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(CURL)
INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIR})
link_directories(curl/bin)
include_directories( ${PROJECT_SOURCE_DIR}/include )
add_executable( http_client src/test.cpp )
target_link_libraries( http_client curl
)
此处坑为,若没有链接curl库文件则报如下错,因此target_link_libraries(** curl)
CMakeFiles/http_client.dir/src/test.cpp.o:在函数‘main’中:
test.cpp:(.text.startup+0x1e):对‘curl_global_init’未定义的引用
test.cpp:(.text.startup+0x23):对‘curl_easy_init’未定义的引用
test.cpp:(.text.startup+0x8c):对‘curl_easy_setopt’未定义的引用
test.cpp:(.text.startup+0x9e):对‘curl_easy_setopt’未定义的引用
test.cpp:(.text.startup+0xb2):对‘curl_easy_setopt’未定义的引用
test.cpp:(.text.startup+0xc6):对‘curl_easy_setopt’未定义的引用
test.cpp:(.text.startup+0xce):对‘curl_easy_perform’未定义的引用
test.cpp:(.text.startup+0x13a):对‘curl_easy_cleanup’未定义的引用
collect2: error: ld returned 1 exit status
CMakeFiles/http_client.dir/build.make:94: recipe for target 'http_client' failed
make[2]: *** [http_client] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/http_client.dir/all' failed
make[1]: *** [CMakeFiles/http_client.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
若无link_directories(curl/bin),则会报错(天坑)
/usr/bin/ld: 找不到 -lCURL
collect2: error: ld returned 1 exit status
CMakeFiles/http_client.dir/build.make:94: recipe for target 'http_client' failed
make[2]: *** [http_client] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/http_client.dir/all' failed
make[1]: *** [CMakeFiles/http_client.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
若服务器端与客户端成功链接,应输出
在HTTP环境中,常常需要将字符串转换为base64编码。之后我们的图像信息也由该编码方式进行传输。该编码方式实现code如下:
base64.h 内容:
#ifndef _BASE64_H_
#define _BASE64_H_
#include
#include
typedef unsigned char BYTE;
std::string base64_encode(BYTE const* bindata, unsigned int binlength);
std::vector base64_decode(std::string const&);
#endif
base64.cpp 内容:
#include
#include "base64.h"
static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static inline bool is_base64(BYTE c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
std::string base64_encode(BYTE const* buf, unsigned int bufLen) {
std::string ret;
int i = 0;
int j = 0;
BYTE char_array_3[3];
BYTE char_array_4[4];
while (bufLen--) {
char_array_3[i++] = *(buf++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}
if (i)
{
for(j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++)
ret += base64_chars[char_array_4[j]];
while((i++ < 3))
ret += '=';
}
return ret;
}
std::vector base64_decode(std::string const& encoded_string) {
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
BYTE char_array_4[4], char_array_3[3];
std::vector ret;
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret.push_back(char_array_3[i]);
i = 0;
}
}
if (i) {
for (j = i; j <4; j++)
char_array_4[j] = 0;
for (j = 0; j <4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++) ret.push_back(char_array_3[j]);
}
return ret;
}
然后将这两个文件添加入客户端与服务器端即可使用。测试例程如下:
clinet端 test.cpp
#include
#include
#include
#include
#include "http.h"
#include "base64.h"
using namespace std;
int env_handler(struct mg_connection *conn);
int main(int argc, char *argv[])
{
CurlHttp curl_http;
string str_url = "http://202.55.666.77:8003";
string str_test = "Hello World!";
string str_test_base64 = base64_encode((BYTE const*)str_test.c_str(), str_test.length()); //转换为base64字符串
while(1)
{
cout << "Post data ...\n";
string result;
int res = curl_http.http_post(str_url.c_str(),str_test_base64.c_str(),&result); //编码后的base64发送至服务器端
cout << "[Response]:" << result << '\n';
sleep(1);
}
return 0;
}
server端 test.cpp
#include
#include
#include
#include
#include "mongoose.h"
#include "base64.h"
using namespace std;
int env_handler(struct mg_connection *conn);
int main(int argc, char *argv[])
{
struct mg_server* server;
server = mg_create_server(NULL); // 初始化一个mongoose server
mg_set_option(server, "listening_port", "8003"); // 设置端口号为8003
mg_add_uri_handler(server, "/", env_handler); // 设置回调函数
printf("Starting on port %s ...\n", mg_get_option(server, "listening_port"));
while (1) {
mg_poll_server(server, 100); // 超时时间(ms)
}
mg_destroy_server(&server);
return 0;
}
int env_handler(struct mg_connection *conn)
{
static int counter = 0;
counter++;
const char* encoded_data = conn->content; // 服务端收到的消息
int encoded_len = conn -> content_len;
string str_encoded(encoded_data,encoded_len);
printf("counter:%d,%s\n",counter,str_encoded.c_str());
vector str_decoded_byte = base64_decode(str_encoded); //base64解码
int decoded_len = str_decoded_byte.size();
string str_decoded;
str_decoded.assign(str_decoded_byte.begin(),str_decoded_byte.end());
mg_printf(conn,"Received:%s,%d",str_decoded.c_str(),counter); //解码后的信息返回客户端
return 0;
}
正常输出应为。服务器端显示”hello world“编码后的base64码"SGVsbG8gV29ybGQh" 而客户端收到的[Response]应为base64码"SGVsbG8gV29ybGQh"解码后的“hello world”。
未完待续......