下面开始用简单但典型的客户端和服务器端程序说明如何进行网络编程。这一小节讲的是客户端,一个用来连接并读取服务器发送来的时间的客户端。
这里涉及到了编写代码,因此要
搭建unix网络编程环境
unix系统我没安装,系统不易获得而且也没必要安装unix。用linux是最佳选择。再者我本来就是ubuntu系统,所以就在ubuntu下搭建环境了。
1,安装编译器,为了齐全还是安装一下build-essential
view plaincopy
sudo apt-get install build-essential
2,下载本书的头文件及示例源码
点此下载unpv12e.tar.gz(这个编译有问题)点此下载unpv13e.tar.gz(这个经itnovice回复中告诉我已经解决了错误,所以最好用这个,能节省很多时间)
(下载unpv13e的话,3中解决问题的地方就可以略过了)
3,解压unpv12e.tar.gz后进入目录查看README,然后可以按照里面提示操作,不过会遇到问题
第一步:在终端中进入upnv12e目录,然后执行代码:
view plaincopy
./configure
第二步:这里有问题及解决方法
view plaincopy
cd lib
view plaincopy
make //这一步我出错了
错误代码为:
view plaincopy
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
redefinition of ‘struct in_pktinfo’的解决方法如下:
结构in_pktinfo已经包含在标准头文件bits/in.h中了, 它又通过 netinet/in.h 在unp.h中被包括进来. 只要将 unp.h 中的结构定义comment out注释掉就行了.
注释掉之后重新
[python] view plaincopy
make //build the basic library that all programs need
这次就成功了!生成了../libunp.a文件。
第三步:这一步没有问题
view plaincopy
cd ../libfree # continue building the basic library
make
第四步:这步解决方法麻烦一点
view plaincopy
cd ../libgai # the getaddrinfo() and getnameinfo() functions
make
错误提示如下:
view plaincopy
gcc -g -O2 -D_REENTRANT -Wall -c -o getaddrinfo.o getaddrinfo.c
getaddrinfo.c: In function ‘getaddrinfo’:
getaddrinfo.c:58: error: ‘EAI_ADDRFAMILY’ undeclared (first use in this function)
getaddrinfo.c:58: error: (Each undeclared identifier is reported only once
getaddrinfo.c:58: error: for each function it appears in.)
getaddrinfo.c:116: error: ‘EAI_NODATA’ undeclared (first use in this function)
make: *** [getaddrinfo.o] 错误 1
解决方法如下:
回到unpv12e目录:
[python] view plaincopy
cd ..
gedit configure.in
找到下面一行注释掉
LIBGAI_OBJS="getaddrinfo.o getnameinfo.o freeaddrinfo.o gai_strerror.o"
然后用下面的代码代替注释掉的那行
LIBGAI_OBJS=
if test "$ac_cv_func_getaddrinfo" = no ; then
LIBGAI_OBJS="$LIBGAI_OBJS getaddrinfo.o"
fi
if test "$ac_cv_func_getnameinfo" = no ; then
LIBGAI_OBJS="$LIBGAI_OBJS getnameinfo.o"
fi
if test "$ac_cv_func_freeaddrinfo" = no ; then
LIBGAI_OBJS="$LIBGAI_OBJS freeaddrinfo.o"
fi
if test "$ac_cv_func_gai_strerror" = no ; then
LIBGAI_OBJS="$LIBGAI_OBJS gai_strerror.o"
fi
view plaincopy
autoconf
./configure
view plaincopy
gedit Make.defines
在文件末尾加上下面这句:
CFLAGS = -g -O2 -D_REENTRANT -Wall -D_GNU_SOURCE
然后重新执行代码:
view plaincopy
cd libgai # continue building the basic library
make
ok,问题解决了!
4,将生成的libunp.a静态库复制到/usr/lib/和/usr/lib64/中。
[html] view plaincopy
cd .. //回到unpv12e目录
[html] view plaincopy
sudo cp libunp.a /usr/lib
sudo cp libunp.a /usr/lib64
5,修改unp.h并将其和config.h拷贝到/usr/include中,为了以后include方便
view plaincopy
gedit lib/unp.h //将unp.h中#include "../config.h"修改为#include "config.h"
view plaincopy
sudo cp lib/unp.h /usr/include
sudo cp config.h /usr/include
6,编译源代码
view plaincopy
cd ./intro
gcc daytimetcpcli.c -o daytimetcpcli -lunp
跟普通的编译不一样的是要在最后加上刚才那个链接库,-l参数加上刚才那个libunp.a去掉lib和后面的.a。最后得到参数-lunp。
7,编写程序
以后编写完的程序安装6中的代码编译就行了,然后代码可以完全跟书上的一样。代码编辑器或IDE等就按自己的喜好来选了。这个网上很多c语言编程环境搭建教程。
我选择命令行用vi编写程序,主要为了锻炼一下自己。
分享到:
上一篇:如何利用Qt Creator打包Meego程序
下一篇:linux各个文件夹代表的意义
顶
10
踩
0
查看评论
5楼 yourihua 2012-08-01 21:39发表 [回复]
网上说是
cd ../libfree
make
inet_ntop.c:61: error: argument 'size' doesn't match prototype
/usr/include/arpa/inet.h:153: error: prototype declaration
inet_ntop.c第61行 size_t size ->改成 socklen_t size
4楼 yourihua 2012-08-01 21:31发表 [回复]
楼主,能帮我看下吗
cd ../libfree
make
3楼 yourihua 2012-08-01 21:31发表 [回复]
administrator@ubuntu:~/workplace/unpv13e/lib$ cd ../libfree
administrator@ubuntu:~/workplace/unpv13e/libfree$ make
gcc -I../lib -g -O2 -D_REENTRANT -Wall -c -o in_cksum.o in_cksum.c
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
/usr/include/arpa/inet.h:65:22: error: prototype declaration
make: *** [inet_ntop.o] 错误 1
administrator@ubuntu:~/workplace/unpv13e/libfree$
2楼 nicker2009 2012-03-15 20:39发表 [回复]
我下载的新的upn13e.tar.gz
之后就是
./configure
cd lib
make
生成了libunp.a , 我将其拷贝到了 /usr/lib 下(我这里没有/usr/lib64/目录),也将config.h 和 unp.h 拷贝到了 /usr/include 目录下
之后再写程序,直接 #include "unp.h" 会提示
undefined reference to `Write(int, void*, unsigned int)'|
...
请教!!!!
Re: a649518776 2012-03-17 23:42发表 [回复]
回复nicker2009:这些Write函数就是一个wrapper函数是吧?所以我用的就是write,然后自己判断是否返回error,自己处理的。用unp的wrapper函数好像还得拷贝一个.a文件,不过我建议你也跟我一样自己写错误处理吧。
1楼 itnovice 2011-09-01 10:40发表 [回复] [引用] [举报]
多谢博文。
在最新的源程序(upn13e.tar.gz) 中, 编译错误已经已经改正了, 你可以试一下。
Re: a649518776 2011-09-01 21:42发表 [回复] [引用] [举报]
回复itnovice:恩,我试了,unpv13e.tar.gz确实没有错误了。早知道就不用这么麻烦了。多谢指教!
分享到:
相关推荐
修改过一些带有编译错误问题的unp.h源码,可在CentOS的服务器上编译,不需要再修改。编译过程如下: tar -zxvf unpv13e.tar.gz rm -y unpv13e.tar.gz cd unpv13e cd lib make cd ../libfree make cd .. cp ...
unp.h 兄弟自己一个字一个打进去的。 之前10分是在没有必要。 资源共享,共同享受编程的乐趣。 这部分代码比较新,可能SCTP有的系统不支持,需要修改一下源码。 如果你觉得还可以,那么给个评论吧。
Unix网络编程卷1,第三版,英文版。大名顶顶的Richard Stevens所写 目录: Copyright Addison-Wesley Professional Computing Series Foreword Preface Introduction Changes from the Second Edition ...
改压缩包包含了Unix网络编程卷1第三版所需的unp.h和对应的头文件。编译环境为x86_64 CentOS7 需要请下载。
the source code of the book "UNIX Network Programming" UNIX Network Programming, Volume 1 This book is for people who want to write programs that communicate with each other using an application ...
通常所说的UNP是网络编程中的经典,必读之书。讲解unix内核中的网络编程,体系结构
unix network programming 源码 unix网络编程源码
文章目录Ubuntu《Unix网络编程》环境搭建下载文件解压缩按照README 依次执行修改inet_ntop.c文件添加到共享库目录修改unp.h程序各文件相对位置case1:case 2:编译执行小问题 下载文件 为了方便获取,我上传到了github ...
unix network program 3e 卷一的源码
unp 经典unix网络编程 个人感觉apue之后看效果更好一些
W. Richard Stevens 的代码
将文件解压到/usr/include/目录下,并根据自己的代码需要,更改unp.h文件中的#include "unp/xxx.c"行,使相应的*.c文件包含在内,别的不做任何更改就可以运行《UNIX网络编程》书里的程序了……
书中源代码作者发布在:www.unpbook.com,https://github.com/unpbook/unpv13e
UNP 源码,建议进阶程序员使用,用处很大
13.6 性能问题 139 13.7 小结 141 第14章 在HTTP服务器上找到的分组 142 14.1 概述 142 14.2 多个HTTP服务器 144 14.3 客户端SYN的到达间隔时间 145 14.4 RTT的测量 149 14.5 用listen设置入连接队列的容量 150 14.6...
英文版,还算清淅 unix networking programming vol2
W. Richard Stevens的原版代码,网络编程的经典巨著
Unix网络编程中unp.h的环境,unpv13e.tar.gz。有些人动不动就20几个积分,像这种开源的资源,也要这么多,真垃圾的很。
unpv13e 并附带了error.c文件,下载下来后,把error.c文件与unp.h和config.h文件一起放到/usr/include/目录下,就防止报err_sys()错误了。