第一章 简介和TCP/IP
2016-06-11 10:03
375 查看
最近在看《Unix网络编程》打算记录下读书过程,用于鞭策自己不要懈怠。
然后解压源代码:tar -xzvf unpv13e.tar.gz
然后查看README文件:
接着按照顺序来执行上面的命令。
我在执行
cd ../libfree
make
之后出错了:
于是,vim inet_ntop.c ,将第60行的 size_t size 改成 socklen_t size; ,再重新make即可。
接下来又出错:
好吧,先把编译问题放到一边,直接
然后发现daytime服务没开启,centos中开启daytime服务:
于是搞定了。
搭建环境
首先是编译环境的搭建,我用的是vm 12 pro+centos7,然后下载源代码 unpv13e.tar.gz然后解压源代码:tar -xzvf unpv13e.tar.gz
然后查看README文件:
QUICK AND DIRTY =============== Execute the following from the src/ directory: ./configure # try to figure out all implementation differences cd lib # build the basic library that all programs need make # use "gmake" everywhere on BSD/OS systems cd ../libfree # continue building the basic library make cd ../libroute # only if your system supports 4.4BSD style routing sockets make # only if your system supports 4.4BSD style routing sockets cd ../libxti # only if your system supports XTI make # only if your system supports XTI cd ../intro # build and test a basic client program make daytimetcpcli ./daytimetcpcli 127.0.0.1 If all that works, you're all set to start compiling individual programs.
接着按照顺序来执行上面的命令。
我在执行
cd ../libfree
make
之后出错了:
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
于是,vim inet_ntop.c ,将第60行的 size_t size 改成 socklen_t size; ,再重新make即可。
接下来又出错:
[root@yao libfree]# cd ../libroute/ [root@yao libroute]# make gcc -I../lib -g -O2 -D_REENTRANT -Wall -c -o get_rtaddrs.o get_rtaddrs.c In file included from get_rtaddrs.c:1:0: unproute.h:3:45: fatal error: net/if_dl.h: No such file or directory #include <net/if_dl.h> /* sockaddr_sdl{} */ ^ compilation terminated. make: *** [get_rtaddrs.o] Error 1
好吧,先把编译问题放到一边,直接
[root@yao unpv13e]# cd intro/ [root@yao intro]# make daytimetcpcli gcc -I../lib -g -O2 -D_REENTRANT -Wall -c -o daytimetcpcli.o daytimetcpcli.c gcc -I../lib -g -O2 -D_REENTRANT -Wall -o daytimetcpcli daytimetcpcli.o ../libunp.a -lpthread [root@yao intro]# ./daytimetcpcli 127.0.0.1 connect error: Connection refused [root@yao intro]# ./daytimetcpcli 127.0.0.1 connect error: Connection refused
然后发现daytime服务没开启,centos中开启daytime服务:
[root@yao intro]# yum install xinetd [root@yao intro]# vim /etc/xinetd.d/daytime-stream //*将disable改为no* [root@yao intro]# vim /etc/xinetd.d/daytime-dgram //*将disable改为no* [root@yao intro]# service xinetd restart Redirecting to /bin/systemctl restart xinetd.service [root@yao intro]# ./daytimetcpcli 127.0.0.1 12 JUN 2016 10:28:58 CST
于是搞定了。
1.2
这里面的好多函数都是后面章节会介绍的,先了解一下。#include "unp.h" int main(int argc, char **argv) { int sockfd, n; char recvline[MAXLINE + 1]; struct sockaddr_in servaddr; if (argc != 2) err_quit("usage: a.out <IPaddress>");//作者编写的函数 if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) err_sys("socket error"); bzero(&servaddr, sizeof(servaddr));//清零 servaddr.sin_family = AF_INET; servaddr.sin_port = htons(13); /* daytime server 端口 */ if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)//将IP转换为合适的格式 err_quit("inet_pton error for %s", argv[1]); if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)//建立连接 err_sys("connect error"); while ( (n = read(sockfd, recvline, MAXLINE)) > 0) { recvline = 0; /* null terminate */ if (fputs(recvline, stdout) == EOF) err_sys("fputs error"); } if (n < 0) err_sys("read error"); exit(0); }
相关文章推荐
- Centos6 编译安装Python
- 硬盘安装CentOS 6.2以及添加GRUB启动菜单
- CentOS 6.2实战部署Nginx+MySQL+PHP
- CentOS 7系统配置上的变化解析
- CentOS下DB2数据库安装过程详解
- CentOS 6.3下编译安装Ruby 2.0笔记
- 在Centos 5.5 上编译安装mysql 5.5.9
- 在阿里云的CentOS环境中安装配置MySQL的教程
- CentOS 6.5源码安装Erlang教程
- Centos下安装docker教程
- 在CenOS系统下安装和配置Redis数据库的教程
- centos 5.6 升级php到5.3的方法
- CentOS mysql安装系统方法
- centos下安装mysql服务器的方法
- CentOS 5.1 4.6最新官方下载地址列表
- Centos搭建GTK+Codeblock完整版
- CentOS 5.1下跑Mono和Asp.net的实现方法
- centos 安装免费控制面板kloxo步骤