nanohttp使用

一、get csoap

官網(wǎng)下載
里面有 nanohttp 的源文件,在 nanohttp 的文件夾中。nanohttp 是 C 語言編寫的,而且很輕量級(jí)。

二、改寫頭文件

將 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 里由于定義了宏,是可以自動(dòng)添加的。這里不用它的Makefile, 因此要自己自動(dòng)添加。

#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>

四、改日志打印級(jí)別(可選)

如果想打出更多的日志的話,可以在 ./example/nanohttp/ 里的http_server.chttp_client.c 兩個(gè)文件中的 main 函數(shù)中,把

/* 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服務(wù)器

對(duì)于 HTTP 服務(wù)器,可添加 :
./example/nanohttp/http_server.c 到 nanohttp 文件夾中。

編譯:注意加 pthread 庫。

sudo gcc *.c -I./ -o server -lpthread
sudo ./server 
執(zhí)行

此時(shí)服務(wù)器已經(jīng)跑起來了,可以在瀏覽器中訪問HTTP服務(wù)器。代碼中默認(rèn)端口是10000,可以通過在代碼中通過 _httpd_port 改寫。


瀏覽器訪問

六、HTTP 客戶端

對(duì)于 HTTP 客戶端,可先刪除掉前面添加到 nanohttp 文件夾中 http_server.c,而添加 :./example/nanohttp/http_client.c 到 nanohttp 文件夾中。

編譯:注意加 pthread 庫。

sudo gcc *.c -I./ -o client -lpthread
sudo ./client http://服務(wù)器IP: 10000
執(zhí)行客戶端
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容