Unix NetWork Programming——环境搭建(解决unp.h等源码编译问题)

原文地址:点击打开链接

下面开始用简单但典型的客户端和服务器端程序说明如何进行网络编程。这一小节讲的是客户端,一个用来连接并读取服务器发送来的时间的客户端。

这里涉及到了编写代码,因此要

搭建unix网络编程环境


unix系统我没安装,系统不易获得而且也没必要安装unix。用linux是最佳选择。再者我本来就是ubuntu系统,所以就在ubuntu下搭建环境了。

1,安装编译器,为了齐全还是安装一下build-essential

  1. sudo apt-get install build-essential  

2,下载本书的头文件及示例源码

点此下载unpv12e.tar.gz(这个编译有问题)点此下载unpv13e.tar.gz(这个经itnovice回复中告诉我已经解决了错误,所以最好用这个,能节省很多时间)


(下载unpv13e的话,3中解决问题的地方就可以略过了)

3,解压unpv12e.tar.gz后进入目录查看README,然后可以按照里面提示操作,不过会遇到问题

第一步:在终端中进入upnv12e目录,然后执行代码:

  1. ./configure  


第二步:这里有问题及解决方法

  1. cd lib  


  1. make  //这一步我出错了  


错误代码为:

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

redefinition of ‘struct in_pktinfo’的解决方法如下: 

结构in_pktinfo已经包含在标准头文件bits/in.h中了, 它又通过 netinet/in.h 在unp.h中被包括进来. 只要将 unp.h 中的结构定义comment out注释掉就行了. 

注释掉之后重新

  1. make //build the basic library that all programs need  


这次就成功了!生成了../libunp.a文件。


第三步:这一步没有问题

  1. cd ../libfree  # continue building the basic library  
  2. make  


第四步:这步解决方法麻烦一点

  1. cd ../libgai   # the getaddrinfo() and getnameinfo() functions  
  2. make  
错误提示如下:
  1. gcc -g -O2 -D_REENTRANT -Wall   -c -o getaddrinfo.o getaddrinfo.c  
  2. getaddrinfo.c: In function ‘getaddrinfo’:  
  3. getaddrinfo.c:58: error: ‘EAI_ADDRFAMILY’ undeclared (first use in this function)  
  4. getaddrinfo.c:58: error: (Each undeclared identifier is reported only once  
  5. getaddrinfo.c:58: error: for each function it appears in.)  
  6. getaddrinfo.c:116: error: ‘EAI_NODATA’ undeclared (first use in this function)  
  7. make: *** [getaddrinfo.o] 错误 1  

解决方法如下:

回到unpv12e目录: 

  1. cd ..  
  2. gedit configure.in   
找到下面一行注释掉
LIBGAI_OBJS="getaddrinfo.o getnameinfo.o freeaddrinfo.o gai_strerror.o"
然后用下面的代码代替注释掉的那行
  1. LIBGAI_OBJS=
  2. if test "$ac_cv_func_getaddrinfo" = no ; then
  3. LIBGAI_OBJS="$LIBGAI_OBJS getaddrinfo.o"
  4. fi
  5. if test "$ac_cv_func_getnameinfo" = no ; then
  6. LIBGAI_OBJS="$LIBGAI_OBJS getnameinfo.o"
  7. fi
  8. if test "$ac_cv_func_freeaddrinfo" = no ; then
  9. LIBGAI_OBJS="$LIBGAI_OBJS freeaddrinfo.o"
  10. fi
  11. if test "$ac_cv_func_gai_strerror" = no ; then
  12. LIBGAI_OBJS="$LIBGAI_OBJS gai_strerror.o"
  13. fi
  1. autoconf  
  2. ./configure  
  1. gedit Make.defines   
在文件末尾加上下面这句:
CFLAGS = -g -O2 -D_REENTRANT -Wall -D_GNU_SOURCE

然后重新执行代码:

  1. cd libgai  # continue building the basic library  
  2. make  

ok,问题解决了!

4,将生成的libunp.a静态库复制到/usr/lib/和/usr/lib64/中。

  1. cd ..   //回到unpv12e目录  
  1. sudo cp libunp.a /usr/lib  
  2. sudo cp libunp.a /usr/lib64  


5,修改unp.h并将其和config.h拷贝到/usr/include中,为了以后include方便
  1. gedit lib/unp.h   //将unp.h中#include "../config.h"修改为#include "config.h"  
  1. sudo cp lib/unp.h /usr/include  
  2. sudo cp config.h /usr/include  

6,编译源代码
  1. cd ./intro  
  2. gcc daytimetcpcli.c -o daytimetcpcli -lunp  

跟普通的编译不一样的是要在最后加上刚才那个链接库,-l参数加上刚才那个libunp.a去掉lib和后面的.a。最后得到参数-lunp。

7,编写程序

以后编写完的程序安装6中的代码编译就行了,然后代码可以完全跟书上的一样。代码编辑器或IDE等就按自己的喜好来选了。这个网上很多c语言编程环境搭建教程。

我选择命令行用vi编写程序,主要为了锻炼一下自己。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UNIX Network Programming Volume 2》(Unix网络编程卷2英文版,djvu格式,带绿色小巧的阅读器) 原书名: UNIX Network Programming Volume 2:Interprocess Communications 2nd ed. 原出版社: Prentice Hall/Pearson 作者: W.Richard Stevens preface part 1. introduction chapter 1. introduction 1.1 introduction 3 1.2 processes, threads, and the sharing of information 5 1.3 persistence of ipc objects 6 1.4 name spaces 7 1.5 effect of fork, exec, and exit on ipc objects 9 1.6 error handling: wrapper functions 11 1.7 unix standards 13 1.8 road map to ipc examples in the text 15 1.9 summary 16 chapter 2. posix ipc 2.1 introduction 19 2.2 ipc names 19 2.3 creating and opening ipc channels 22 2.4 ipc permissions 25 2.5 summary 26 .chapter 3. system v ipc 3.1 introduction 27 3.2 key_t keys and ftok function 28 3.3 ipc_perm structure 30 3.4 creating and opening ipc channels 30 3.5 ipc permissions 32 3.6 identifier reuse 34 3.7 ipcs and ipcrm programs 36 3.8 kernel limits 36 3.9 summary 38 part 2. message passing chapter 4. pipes and fifos 4.1 introduction 43 4.2 a simple client--server example 43 4.3 pipes 44 4.4 full-duplex pipes 50 4.5 popen and pc1ose functions 52 4.6 fifos 54 4.7 additional properties of pipes and fifos 58 4.8 one server, multiple clients 60 4.9 iterative versus concurrent servers 66 4.10 streams and messages 67 4.11 pipe and fifo limits 72 4.12 summary 73 chapter 5. posix message oueues 5.1 introduction 75 5.2 mq_open, mq_c1ose, and mq_un1ink functions 76 5.3 mq_getattr and mq_setattr functions 79 5.4 mq_send and mq_receive functions 82 5.5 message queue limits 86 5.6 mq_notify function 87 5.7 posix realtime signals 98 5.8 implementation using memory-mapped i/o 106 5.9 summary 126 chapter 6. system v message queues 6.1 introduction 129 6.2 msgget function 130 6.3 msgsnd function 131 6.4 msgrcv function 132 6.5 msgctl function 134 6.6 simple programs 135 6.7 client-server example 140 6.8 multiplexing messages 142 6.9 message queues w

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值