最近在学习网络编程相关知识,遂有想法做点小东西来练手,大致框架为,TX2板子搭载深度相机获取图像信息,本地运行ORB_SLAM解算位姿信息,传输OPENCV_MAT格式图像信息至服务器运行yolo3识别目标物体,返回像素坐标,对下一步小车运行轨迹进行导航规划。
现在搭建HTTP协议框架实现传输图像信息至服务器功能
一、sever/服务器端程序搭建
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 <new-branch-name>
HEAD 目前位于 fa35195... 5.0 -> 5.1
2.新建工程文件,将mongoose中的mongoose.c与mongoose.h添加至工程文件中。
本人测试工程文件目录如下:
其中test.cpp文件为
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <iostream>
#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