《UNIX網路程式設計 卷1:套接字聯網API》第一個例子執行時環境搭建

KindSuper_liu發表於2020-11-08
redhat@redhat-virtual-machine:~/桌面/unpv13e$ ./configure    

redhat@redhat-virtual-machine:~/桌面/unpv13e$ cd ./lib
redhat@redhat-virtual-machine:~/桌面/unpv13e/lib$ make


redhat@redhat-virtual-machine:~/桌面/unpv13e$ cd ./libfree
redhat@redhat-virtual-machine:~/桌面/unpv13e/libfree$ make
//    如果報錯如下,則需要在當前目錄下開啟inet_ntop.c檔案
//    將第60行的size_t size修改為socklen_t size 然後儲存
//    重新輸入make後不報錯即可

gcc -I../lib -g -O2 -D_REENTRANT -Wall   -c -o inet_ntop.o inet_ntop.c  
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,  
                    ^  
make: *** [inet_ntop.o] Error 1  


redhat@redhat-virtual-machine:~/桌面/unpv13e/libfree$ cd ../libgai
redhat@redhat-virtual-machine:~/桌面/unpv13e/libgai$ make
//    以下的warning不用理會
/usr/include/arpa/inet.h: In function ‘inet_ntop’:  
inet_ntop.c:152:23: warning: ‘best.len’ may be used uninitialized in this function [-Wmaybe-uninitialized]  
   if (best.base == -1 || cur.len > best.len)  
                       ^  
inet_ntop.c:123:28: note: ‘best.len’ was declared here  
  struct { int base, len; } best, cur;  
                            ^  
gcc -I../lib -g -O2 -D_REENTRANT -Wall   -c -o inet_pton.o inet_pton.c  
ar rv ../libunp.a in_cksum.o inet_ntop.o inet_pton.o  
a - in_cksum.o  
a - inet_ntop.o  
a - inet_pton.o  
ranlib ../libunp.a  

//    用root許可權將以上編譯生成的libunp.a 檔案複製到/usr/lib目錄中

redhat@redhat-virtual-machine:~/桌面/unpv13e/libgai$ cd ..
redhat@redhat-virtual-machine:~/桌面/unpv13e$ sudo cp libunp.a /usr/lib
[sudo] redhat 的密碼: 

//    開啟unp.h檔案將其中的#include "../config.h" 改成 #include "config.h"
redhat@redhat-virtual-machine:~/桌面/unpv13e$ vim lib/unp.h 

//    進入intro目錄編譯客戶端檔案並用root許可權執行
redhat@redhat-virtual-machine:~/桌面/unpv13e$ cd intro/
redhat@redhat-virtual-machine:~/桌面/unpv13e/intro$  make daytimetcpcli
redhat@redhat-virtual-machine:~/桌面/unpv13e/intro$  sudo ./daytimetcpcli 127.0.0.1
//    錯誤提示無法連線
connect error: Connection refused

//    我們先開啟伺服器
redhat@redhat-virtual-machine:~/桌面/unpv13e/intro$ make daytimetcpsrv
redhat@redhat-virtual-machine:~/桌面/unpv13e/intro$ sudo ./daytimetcpsrv

//    然後再開啟另一個終端,在那裡再執行客戶端即可
redhat@redhat-virtual-machine:~/桌面/unpv13e/intro$ sudo ./daytimetcpcli 127.0.0.1
[sudo] redhat 的密碼: 
Mon Dec 25 21:00:36 2017

相關文章