UNP環境配置

最近在學習《UNIX網絡編程》,書上將經常使用的頭文件都放在unp.h裏,須要本身編譯一下代碼搭建環境。html

UNP環境配置過程


  1. 下載源碼 http://www.unpbook.com/src.html
  2. 按照README的說明進行編譯
$ ./configure
$ cd lib
$ make
$ cd ../libfree
$ make

此時,會在unpv13e文件夾下產生一個libunp.a的庫文件編程

  1. 測試編譯是否成功
$ cd ../intro    # build and test a basic client program
$ make daytimetcpcli
$ ./daytimetcpcli 127.0.0.1

成功以後會顯示以下:bash

$ ./daytimetcpcli 127.0.0.1
02 MAY 2015 12:05:03 CST
  1. 將libunp.a放置到/usr/lib/與/usr/lib64/文件夾下,並修改環境變量。將config.h與unp.h放置到源碼文件夾,修改unp.h中的../config.h爲config.h.
  2. 開始練習
$ gcc daytimetcpcli.c -lunp

ps:碰到的問題


1. libfree編譯錯誤

出現以下報錯:網絡

inet_ntop.c:61: error: argument 'size' doesn't match prototype
/usr/include/arpa/inet.h:153: error: prototype declaration

處理:
修改libfree文件夾下的inet_ntop.c第61行 size_t size ->改爲 socklen_t sizetcp

2. daytimetcpcli出現connected refused錯誤

出現以下報錯:學習

[root@localhost intro]# ./daytimetcpcli 127.0.0.1
connect error: Connection refused

處理:測試

  1. 安裝xinetd
  2. 修改 /etc/xinetd.d/daytime文件中的disable yesdisable no
  3. 重啓xinetd
$ /etc/xinetd.d/ restart
  1. 查看是否開啓daytime
$ netstat -ant

開啓daytime服務後,會有13這個端口ui

tcp        0      0 0.0.0.0:13              0.0.0.0:*               LISTEN

3. redefinition of ‘struct in_pktinfo’錯誤

出現以下報錯:prototype

gcc -g -O2 -D_REENTRANT -Wall   -c -o connect_nonb.o connect_nonb.c  
    In file included from connect_nonb.c:1:  
    unp.h:114: error: redefinition of ‘struct in_pktinfo’  
    make: *** [connect_nonb.o] 錯誤 1

處理:
結構in_pktinfo已經包含在標準頭文件bits/in.h中了, 它又經過 netinet/in.h 在unp.h中被包括進來. 只要將 unp.h 中的結構定義comment out註釋掉,而後從新編譯libunp.arest

相關文章
相關標籤/搜索