首页 > 代码库 > APUE16章的运行示例16-14
APUE16章的运行示例16-14
参考文章:http://blog.csdn.net/andyxie407/article/details/1672325
今天在运行在APUE第16章的16-14(客户端)和16-15(服务端)遇到了不少问题,搞了半天,运行方法多谢andyxie407的文章给了很好地参考(本来就一样,不叫参考),后面还是运行不出结果,结果找到了两个程序抄错了的地方(原谅我,有时候,看一眼大概实现就把代码敲上去了),写都写到这了,还是把我的痛苦运行结果展示一番,代码如下:
- //16-14.c 即客户端
- #include "apue.h"
- #include <netdb.h>
- #include <errno.h>
- #include <sys/socket.h>
- #define MAXADDRLEN 256
- #define BUFLEN 128
- #define MAXSLLEP 128
- int connect_retry(int sockfd, struct sockaddr *addr, socklen_t alen)
- {
- int nsec;
- for ( nsec = 1; nsec < MAXSLLEP; nsec <<= 1) {//nsec <<= 1 equal to nsec = nsec << 1 equal to nsec /= 2;
- if (connect(sockfd, addr, alen) == 0)
- return 0;
- if (nsec <= MAXSLLEP/2)
- sleep(nsec);
- }
- return -1;
- }
- void print_uptime(int sockfd)
- {
- char buf[BUFLEN];
- int n;
- while ((n = recv(sockfd, buf, BUFLEN, 0)) > 0)
- write(STDOUT_FILENO, buf, n);
- if (n < 0)
- err_sys("recv error");
- }
- int main(int argc, char *argv[])
- {
- struct addrinfo *aip, *ailist;
- struct addrinfo hint;
- int err, sockfd;
- if (argc != 2)
- err_quit("Usage: ruptime hostname");
- hint.ai_family = 0;
- hint.ai_flags = 0;//AI_CANONNAME;
- hint.ai_socktype = SOCK_STREAM;
- hint.ai_protocol = 0;
- hint.ai_addrlen = 0;
- hint.ai_canonname = NULL;
- hint.ai_addr = NULL;
- hint.ai_next = NULL;
- if ((err = getaddrinfo(argv[1], "ruptime", &hint, &ailist)) != 0)
- err_quit("getaddrinfo:%s", gai_strerror(err));
- for (aip = ailist; aip != NULL; aip = aip->ai_next) {
- if ((sockfd = socket(aip->ai_family, SOCK_STREAM, 0)) < 0) {
- err = errno;
- }
- if (connect_retry(sockfd, aip->ai_addr, aip->ai_addrlen) < 0)
- err = errno;
- else {
- printf("connect to ruptime server successfully !\n");
- print_uptime(sockfd);
- exit(0);
- }
- }
- fprintf(stderr, "cannot connect to %s:%s\n", argv[1], strerror(err));
- exit(1); //err happened
- }
- //16-15.c即服务端
- #include "apue.h"
- #include <netdb.h>
- #include <syslog.h>
- #include <errno.h>
- #include <sys/socket.h>
- #define BUFLEN 128
- #define QLEN 10
- #ifndef HOST_NAME_MAX
- #define HOST_NAME_MAX 256
- #endif
- int init_server(int type, struct sockaddr *addr, socklen_t alen, int qlen)
- {
- int fd, err = 0;
- if ((fd = socket(addr->sa_family, type, 0)) < 0)
- return -1;
- if (bind(fd, addr, alen) < 0) {
- err = errno;
- goto errout;
- }
- if (type == SOCK_STREAM || type == SOCK_SEQPACKET) {
- if (listen(fd, QLEN) < 0) {
- err = errno;
- goto errout;
- }
- }
- return fd; //successful when prog arrive here.
- errout:
- close(fd); //exit with failuare
- errno = err;
- return -1;
- }
- void serve(int fd)
- {
- char buf[BUFLEN];
- int clfd;
- FILE *fp;
- for ( ; ; ) {
- if ((clfd = accept(fd, NULL, NULL)) < 0) {
- syslog(LOG_ERR, "ruptime: accept error:%s", strerror(errno));
- exit(1);
- }
- if ((fp = popen("/usr/bin/uptime", "r")) == NULL) {
- sprintf(buf, "error:%s\n", strerror(errno));
- send(clfd, buf, strlen(buf), 0);
- } else {
- while (fgets(buf, BUFLEN, fp) != NULL)
- send(clfd, buf, strlen(buf), 0);
- pclose(fp);
- }
- close(clfd); //send end
- }
- }
- int main(int argc, char *argv[])
- {
- struct addrinfo *ailist, *aip;
- struct addrinfo hint;
- int err, sockfd, n;
- char *host;
- if (argc != 1)
- err_quit("Usage:service");
- #ifdef _SC_HOST_NAME_MAX
- n = sysconf(_SC_HOST_NAME_MAX);
- if (n < 0)
- #endif
- n = HOST_NAME_MAX;
- host = malloc(n);
- if (!host)
- err_sys("malloc error");
- if (gethostname(host, n) < 0)
- err_sys("gethostname");
- printf("host name is:%s\n", host);
- daemonize("ruptimed");
- hint.ai_flags = AI_CANONNAME;
- hint.ai_family = 0;
- hint.ai_socktype = SOCK_STREAM;
- hint.ai_protocol = 0;
- hint.ai_addrlen = 0;
- hint.ai_canonname = NULL;
- hint.ai_addr = NULL;
- hint.ai_next = NULL;
- if ((err = getaddrinfo(host, "ruptime", &hint, &ailist)) != 0) {
- syslog(LOG_ERR , "ruptime:getaddrinfo error:%s", gai_strerror(err));
- exit(1);
- }
- for (aip = ailist; aip != NULL; aip = aip->ai_next) {
- sockfd = init_server(SOCK_STREAM, aip->ai_addr, aip->ai_addrlen, QLEN);
- if (sockfd >= 0){
- serve(sockfd);
- exit(0);
- }
- }
- exit(1);
- }
分别编译成可执行文件,然后运行服务端 ./ruptimed,可以通过/var/log/syslog看到发生了错误
Servname not supported for ai_socktype .
解决方法之一:先到/etc/services中添加如下内容
ruptime 4000/tcp
保存离开,但是你要确保4000这个端口没有被其他服务占用,否则改用其他端口。这时先运行
$./ruptimed
这是16-15编译出的名字,可以通过ps -ef 看到刚才的程序已成为后台程序,再运行
$./ruptime ubuntu
前面那个是16-14编译出的名字,后面那个是用户/主机名(前面有篇文章讲到了,但是这里不能用localhost或127.0.0.1
得到结果。
12:06:45 up 21:40, 2 users, load average: 0.00, 0.01, 0.05
很兴奋有木有!
还有一种方法不用更改/etc/services的方法,是将程序中的服务名改成端口号,然后改些其他内容,详见参考文章!
顺便提醒自己一句:在你不能保证自己程序的正确性时(即便编译通过不能代表没有写错或写漏一些东西),先不要到处找程序之外的任何原因,谨记!
APUE16章的运行示例16-14