unix網絡編程環境搭建


unix網絡編程環境搭建

1.點擊下載源代碼

可以通過下列官網中的源代碼目錄下載最新代碼:
http://www.unpbook.com/src.html

2.解壓文件

  1. tar -xzvf upv13e.tar.gz 

3.上傳至阿里雲

本人本地已經配置好,這次實驗是將環境搭建至雲服務器中。

  1. scp -r unpv13e root@120.76.140.119:/root/program/unp 
  2. // -r 上傳文件夾 

4.編譯文件

  1. cd unpv13e 
  2. chmod a+x configure //非root用戶需要賦予可執行能力 
  3. ./configure 
  4. cd lib //進入lib目錄並且編譯 此為編譯庫文件  
  5. make 
  6. cd libfree //進入libfree目錄並且編譯 
  7. make 

libfree編譯會出現下列問題:

  1. inet_ntop.c: In function ‘inet_ntop’: 
  2. inet_ntop.c:60:9: error: argument ‘size’ doesn’t match prototype 
  3. size_t size; 

  4. In file included from inet_ntop.c:27:0
  5. /usr/include/arpa/inet.h:64:20: error: prototype declaration 
  6. extern const char *inet_ntop (int __af, const void *__restrict __cp, 

  7. <builtin>: recipe for target 'inet_ntop.o' failed 
  8. 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中加入如下代碼即可:(插入到所有頭文件之后)

  1. #define size_t socklen_t 

此時make就能成功。

5.測試introduction實例

1.使用make命令進行測試

  1. cd intro 
  2. make daytimetcpsrv 
  3. make daytimetcpcli 
  4. //測試結果 
  5. ./daytimetcpsrv 
  6. ./daytimetcpcli 127.0.0.1 
  7. Sat May 6 21:35:10 2017 

2.使用gcc編譯進行測試

  1. //拷貝libunp.a到庫文件夾目錄/usr/lib,便於使用-lunp命令 
  2. cp libunp.a /usr/lib 
  3. //修改頭文件unp.h,並將unp.h和config.h拷貝到/usr/include 
  4. cp /lib/unp.h /usr/include 
  5. cp config.h /usr/include 
  6. //由於config.h與unp.h在同一個目錄夾下 
  7. vim /usr/include/unp.h 
  8. //修改include "../config.h"成“config.h" 

編譯測試代碼

  1. cd ./intro  
  2. gcc daytimetcpcli.c -o daytimetcpcli -lunp 
  3. gcc daytimetcpsrv.c -o daytimetcpsrv -lunp 
  4. ./daytimetcpsrv 
  5. ./daytimetcpcli 127.0.0.1 
  6. Sat May 6 21:35:10 2017 

-lworld表示在上面的lib的路徑中尋找libworld.so動態庫文件(如果gcc編譯選項中加入了“-static”表示尋找libworld.a靜態庫文件)

6.如果需要關閉服務器程序,需要使用下面方法

  1. netstat -anp |grep daytimetcpsrv //具體含義使用--help查看,找到pidnum 
  2. kill -9 pidnum 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM