一、get csoap
官網(wǎng)下載
里面有 nanohttp 的源文件,在 nanohttp 的文件夾中。nanohttp 是 C 語言編寫的,而且很輕量級(jí)。
二、改寫頭文件
將 nanohttp 文件夾中的所有頭文件及 ./example/nanohttp/ 中的 http_server.c 和 http_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.c 和 http_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í)行客戶端