nanohttp使用

一、get csoap

官网下载
里面有 nanohttp 的源文件,在 nanohttp 的文件夹中。nanohttp 是 C 语言编写的,而且很轻量级。

二、改写头文件

将 nanohttp 文件夹中的所有头文件及 ./example/nanohttp/ 中的 http_server.chttp_client.c 改写正确,像下面这种:

#include <nanohttp/nanohttp-common.h>

要改写成为:

#include "nanohttp-common.h"

三、include 头文件

由于只想要其中的 nanohttp 部分,不想要 soap 部分,因此可以不按其自带的 Makefile 去编绎。
直接在 nanohttp-logging.h 中,添加如下头文件。注:这些头文件在其自带的 Makefile 里由于定义了宏,是可以自动添加的。这里不用它的Makefile, 因此要自己自动添加。

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <fcntl.h>

四、改日志打印级别(可选)

如果想打出更多的日志的话,可以在 ./example/nanohttp/ 里的http_server.chttp_client.c 两个文件中的 main 函数中,把

/* Set log level to see more information written by the library */
hlog_set_level(HLOG_INFO);

改成

/* Set log level to see more information written by the library */
hlog_set_level(HLOG_VERBOSE);    //差不多打印所有日志

五、HTTP服务器

对于 HTTP 服务器,可添加 :
./example/nanohttp/http_server.c 到 nanohttp 文件夹中。

编译:注意加 pthread 库。

sudo gcc *.c -I./ -o server -lpthread
sudo ./server 
执行

此时服务器已经跑起来了,可以在浏览器中访问HTTP服务器。代码中默认端口是10000,可以通过在代码中通过 _httpd_port 改写。


浏览器访问

六、HTTP 客户端

对于 HTTP 客户端,可先删除掉前面添加到 nanohttp 文件夹中 http_server.c,而添加 :./example/nanohttp/http_client.c 到 nanohttp 文件夹中。

编译:注意加 pthread 库。

sudo gcc *.c -I./ -o client -lpthread
sudo ./client http://服务器IP: 10000
执行客户端
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容