unix網絡編程環境搭建
1.點擊下載源代碼
可以通過下列官網中的源代碼目錄下載最新代碼:
http://www.unpbook.com/src.html
2.解壓文件
- tar -xzvf upv13e.tar.gz
3.上傳至阿里雲
本人本地已經配置好,這次實驗是將環境搭建至雲服務器中。
- scp -r unpv13e root@120.76.140.119:/root/program/unp
- // -r 上傳文件夾
4.編譯文件
- cd unpv13e
- chmod a+x configure //非root用戶需要賦予可執行能力
- ./configure
- cd lib //進入lib目錄並且編譯 此為編譯庫文件
- make
- cd libfree //進入libfree目錄並且編譯
- make
libfree編譯會出現下列問題:
- inet_ntop.c: In function ‘inet_ntop’:
- inet_ntop.c:60:9: error: argument ‘size’ doesn’t match prototype
- size_t size;
- ^
- In file included from inet_ntop.c:27:0:
- /usr/include/arpa/inet.h:64:20: error: prototype declaration
- extern const char *inet_ntop (int __af, const void *__restrict __cp,
- ^
- <builtin>: recipe for target 'inet_ntop.o' failed
- make: *** [inet_ntop.o] Error 1
錯誤提示inet_ntop.c中60行聲明與原型申明/usr/include/arpa/inet.h不匹配。(#include一般所在文件都在/usr/include中)
經查驗,最后一個參數,在inet.h中定義socklen_t,而inet_ntop.c中定義為size_t。在inet_ntop.c中加入如下代碼即可:(插入到所有頭文件之后)
- #define size_t socklen_t
此時make就能成功。
5.測試introduction實例
1.使用make命令進行測試
- cd intro
- make daytimetcpsrv
- make daytimetcpcli
- //測試結果
- ./daytimetcpsrv
- ./daytimetcpcli 127.0.0.1
- Sat May 6 21:35:10 2017
2.使用gcc編譯進行測試
- //拷貝libunp.a到庫文件夾目錄/usr/lib,便於使用-lunp命令
- cp libunp.a /usr/lib
- //修改頭文件unp.h,並將unp.h和config.h拷貝到/usr/include
- cp /lib/unp.h /usr/include
- cp config.h /usr/include
- //由於config.h與unp.h在同一個目錄夾下
- vim /usr/include/unp.h
- //修改include "../config.h"成“config.h"
編譯測試代碼
- cd ./intro
- gcc daytimetcpcli.c -o daytimetcpcli -lunp
- gcc daytimetcpsrv.c -o daytimetcpsrv -lunp
- ./daytimetcpsrv
- ./daytimetcpcli 127.0.0.1
- Sat May 6 21:35:10 2017
-lworld表示在上面的lib的路徑中尋找libworld.so動態庫文件(如果gcc編譯選項中加入了“-static”表示尋找libworld.a靜態庫文件)
6.如果需要關閉服務器程序,需要使用下面方法
- netstat -anp |grep daytimetcpsrv //具體含義使用--help查看,找到pidnum
- kill -9 pidnum