UNIX网络编程 卷2 源代码使用

简介: 1. 下载源码,W. Richard Stevens的主页:http://www.kohala.com/start/ wget http://www.kohala.com/start/unpv22e/unpv22e.
1. 下载源码,W. Richard Stevens的主页:http://www.kohala.com/start/
wget http://www.kohala.com/start/unpv22e/unpv22e.tar.gz -P /usr/local/src 
2. 解压
tar xvf /usr/local/src/unpv22e.tar.gz -C /root/bin 
3. 编译库文件
cd /root/bin/unpv22e/  
./configure 
编辑生成config.h文件,注释以下几行
vi config.h  
56 // #define uint8_t unsigned char  
57 // #define uint16_t unsigned short  
58 // #define uint32_t unsigned int  
添加MSG_R和MSG_W定义
vi config.h  
66 // add by jcq  
67 typedef unsigned long ulong_t;  
68 #define MSG_R 0400  
69 #define MSG_W 0200 
添加_GNU_SOURCE定义
vi config.h  
#define _GNU_SOURCE 
修改warpunix.c中的MKtemp函数,使用mkstemp函数替换mktemp函数
cd lib  
181 void  
182 Mktemp(char *template)  
183 {  
184 if (mkstemp(template) == NULL || template[0] == 0)  
185 err_quit("mktemp error");  
186 } 
编译生成libunpipc.a
cd lib  
make
 
将生成的libunpipc.a复制到/usr/lib中。
[root@localhost myunp2]# vim ftok.c
[root@localhost myunp2]# gcc -o ftok ftok.c 
/tmp/ccxRydhw.o: In function `main':
ftok.c:(.text+0x18): undefined reference to `err_quit'
ftok.c:(.text+0x30): undefined reference to `Stat'
ftok.c:(.text+0x48): undefined reference to `Ftok'
collect2: 错误:ld 返回 1
[root@localhost myunp2]# gcc -o ftok ftok.c -lunpipc
/usr/lib/libunpipc.a(wrapunix.o): In function `Mq_open':
wrapunix.c:(.text+0x4d8): undefined reference to `mq_open'
wrapunix.c:(.text+0x4f0): undefined reference to `mq_open'
/usr/lib/libunpipc.a(wrapunix.o): In function `Mq_close':
wrapunix.c:(.text+0x52b): undefined reference to `mq_close'
/usr/lib/libunpipc.a(wrapunix.o): In function `Mq_unlink':
wrapunix.c:(.text+0x55b): undefined reference to `mq_unlink'
/usr/lib/libunpipc.a(wrapunix.o): In function `Mq_send':
wrapunix.c:(.text+0x5a3): undefined reference to `mq_send'
/usr/lib/libunpipc.a(wrapunix.o): In function `Mq_receive':
wrapunix.c:(.text+0x5e3): undefined reference to `mq_receive'
/usr/lib/libunpipc.a(wrapunix.o): In function `Mq_notify':
wrapunix.c:(.text+0x623): undefined reference to `mq_notify'
/usr/lib/libunpipc.a(wrapunix.o): In function `Mq_getattr':
wrapunix.c:(.text+0x653): undefined reference to `mq_getattr'
/usr/lib/libunpipc.a(wrapunix.o): In function `Mq_setattr':
wrapunix.c:(.text+0x68b): undefined reference to `mq_setattr'
/usr/lib/libunpipc.a(wrapunix.o): In function `Sem_open':
wrapunix.c:(.text+0x9c8): undefined reference to `sem_open'
wrapunix.c:(.text+0x9e0): undefined reference to `sem_open'
/usr/lib/libunpipc.a(wrapunix.o): In function `Sem_close':
wrapunix.c:(.text+0xa1b): undefined reference to `sem_close'
/usr/lib/libunpipc.a(wrapunix.o): In function `Sem_unlink':
wrapunix.c:(.text+0xa4b): undefined reference to `sem_unlink'
/usr/lib/libunpipc.a(wrapunix.o): In function `Sem_init':
wrapunix.c:(.text+0xa8b): undefined reference to `sem_init'
/usr/lib/libunpipc.a(wrapunix.o): In function `Sem_destroy':
wrapunix.c:(.text+0xabb): undefined reference to `sem_destroy'
/usr/lib/libunpipc.a(wrapunix.o): In function `Sem_wait':
wrapunix.c:(.text+0xaeb): undefined reference to `sem_wait'
/usr/lib/libunpipc.a(wrapunix.o): In function `Sem_trywait':
wrapunix.c:(.text+0xb1c): undefined reference to `sem_trywait'
/usr/lib/libunpipc.a(wrapunix.o): In function `Sem_post':
wrapunix.c:(.text+0xb5b): undefined reference to `sem_post'
/usr/lib/libunpipc.a(wrapunix.o): In function `Sem_getvalue':
wrapunix.c:(.text+0xb93): undefined reference to `sem_getvalue'
/usr/lib/libunpipc.a(wrapunix.o): In function `Shm_open':
wrapunix.c:(.text+0xccc): undefined reference to `shm_open'
/usr/lib/libunpipc.a(wrapunix.o): In function `Shm_unlink':
wrapunix.c:(.text+0xd0b): undefined reference to `shm_unlink'
collect2: 错误:ld 返回 1
[root@localhost myunp2]# gcc -lrt -o ftok ftok.c -lunpipc
[root@localhost myunp2]# man mq_open
Cannot open the message catalog "man" for locale "zh_CN.UTF-8"
(NLSPATH="/usr/share/locale/%l/LC_MESSAGES/%N")

Formatting page, please wait...

出现上面的错误,使用gcc 加选项-lrt解决。

 

错误:

[root@localhost myunp2]# gcc -lrt -o slot slot.c -lunpipc
slot.c: 在函数‘main’中:
slot.c:8:22: 错误:‘IPC_PRIVATE’未声明(在此函数内第一次使用)
         msqid=Msgget(IPC_PRIVATE,SVMSG_MODE|IPC_CREAT);
                      ^
slot.c:8:22: 附注:每个未声明的标识符在其出现的函数内只报告一次
In file included from slot.c:1:0:
unpipc.h:153:21: 错误:‘MSG_R’未声明(在此函数内第一次使用)
 #define SVMSG_MODE (MSG_R | MSG_W | MSG_R>>3 | MSG_R>>6)
                     ^
slot.c:8:34: 附注:in expansion of macro ‘SVMSG_MODE’
         msqid=Msgget(IPC_PRIVATE,SVMSG_MODE|IPC_CREAT);
                                  ^
unpipc.h:153:29: 错误:‘MSG_W’未声明(在此函数内第一次使用)
 #define SVMSG_MODE (MSG_R | MSG_W | MSG_R>>3 | MSG_R>>6)
                             ^
slot.c:8:34: 附注:in expansion of macro ‘SVMSG_MODE’
         msqid=Msgget(IPC_PRIVATE,SVMSG_MODE|IPC_CREAT);
                                  ^
slot.c:8:45: 错误:‘IPC_CREAT’未声明(在此函数内第一次使用)
         msqid=Msgget(IPC_PRIVATE,SVMSG_MODE|IPC_CREAT);
                                             ^
slot.c:11:22: 错误:‘IPC_RMID’未声明(在此函数内第一次使用)
         Msgctl(msqid,IPC_RMID,NULL);

解决方法:

1.unpv22e里面的Make.defines 
修改 
#CFLAGS = -g -O2 -D_REENTRANT -Wall 
CFLAGS = -g -O2 -D_GNU_SOURCE -D__USE_GNU -D_REENTRANT -Wall 

2 将unpv22e中的unpipc.h拷贝到需要编译的代码的目录。

3 将config.h也拷贝到这个目录,由于unpipc.h依赖于这个头文件。

 

相关实践学习
RocketMQ一站式入门使用
从源码编译、部署broker、部署namesrv,使用java客户端首发消息等一站式入门RocketMQ。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
4月前
|
缓存 网络协议 Unix
Linux(UNIX)五种网络I/O模型与IO多路复用
Linux(UNIX)五种网络I/O模型与IO多路复用
107 0
|
3月前
|
网络协议 Unix Linux
在Unix/Linux shell中,与网络相关的命令
在Unix/Linux shell中,与网络相关的命令
24 2
|
9月前
|
机器学习/深度学习 数据可视化 数据库
【MATLAB第5期】源码分享#基于小波时间散射网络(WTSN)和长短期记忆网络 (LSTM) 的ECG信号分类模型,含源代码+中文注释,保姆级教学
【MATLAB第5期】源码分享#基于小波时间散射网络(WTSN)和长短期记忆网络 (LSTM) 的ECG信号分类模型,含源代码+中文注释,保姆级教学
|
9月前
|
机器学习/深度学习 数据采集 算法
【MATLAB第4期】源码分享#基于贝叶斯Bayes算法优化LSTM长短期记忆网络的时间序列预测模型,含源代码+中文注释,保姆级教学
【MATLAB第4期】源码分享#基于贝叶斯Bayes算法优化LSTM长短期记忆网络的时间序列预测模型,含源代码+中文注释,保姆级教学
|
缓存 安全 网络协议
【Unix/Linux 系统管理】内容分发网络
【Unix/Linux 系统管理】内容分发网络
112 0
|
机器学习/深度学习 算法 Python
python实现BP神经网络进行预测和误差分析(附源代码)
python实现BP神经网络进行预测和误差分析(附源代码)
839 0
python实现BP神经网络进行预测和误差分析(附源代码)
|
设计模式 安全 网络协议
Unix 网络 IO 模型
文件描述符(file descriptor,简称 fd)在形式上是一个非负整数。实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表。当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符。在程序设计中,一些涉及底层的程序编写往往会围绕着文件描述符展开。但是文件描述符这一概念往往只适用于 UNIX、Linux 这样的操作系统。
Unix 网络 IO 模型
|
缓存 网络协议 安全
Java 的I/O、NIO ,Java IO 模型,Unix 网络 IO 模型等相关概念的解析
java Io流共涉及40多个类,这些类看上去很杂乱,但实际上很有规则,而且彼此之间存在非常紧密的联系, Java Io流的40多个类都是从如下4个抽象类基类中派生出来的。
Java 的I/O、NIO ,Java IO 模型,Unix 网络 IO 模型等相关概念的解析
|
网络协议 Unix
UNIX网络编程卷1(第三版) 客户/服务器程序示例
UNIX网络编程卷1(第三版) 客户/服务器程序示例
89 0

热门文章

最新文章