LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1170|回复: 9

(已解决,9楼是我的一些心得)如何添加自己的服务

[复制链接]
发表于 2007-6-20 07:12:56 | 显示全部楼层 |阅读模式
有这样一个C/S程序,server提供一个叫做ruptime的服务,功能是当有客户端连接时调用uptime程序,并将结果发送到client。可是现在的问题是,这个服务系统本来是没有的,所以调用getaddrinfo的时候会返回如下错误:
  1. Servname not supported for ai_socktype
复制代码

我觉得可能是需要编辑/etc/service文件把自己这个服务加进去,可是我加了之后没管用(设的端口是4000),开机的时候提示启动服务失败,所以我的问题就是如何开启我这个服务器程序提供的服务,谢谢。

PS:不知道这个帖子发到这个版合不合适,不合适的话麻烦版主帮忙转一下,不过代码是没有问题的。
发表于 2007-6-20 07:31:21 | 显示全部楼层
自己选一个端口号4000,直接bind, listen不行?服务不服务只是一个花头。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-6-20 07:55:35 | 显示全部楼层
早啊,兄弟,你说的方法当然可以,但关键是我想搞明白怎样以服务的形式运行这段代码,怎样做才能让getaddrinfo执行正确。下面这段是服务器代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <unistd.h>
  6. #include <syslog.h>
  7. #include <netdb.h>
  8. #include <sys/socket.h>

  9. #ifndef        HOST_NAME_MAX
  10. #define        HOST_NAME_MAX        64
  11. #endif

  12. #define        BUFLEN                128
  13. #define        QLEN                10

  14. extern int initserver(int, const struct sockaddr *, socklen_t, int);
  15. extern void daemonize(const char *);

  16. void
  17. serve(int sockfd)
  18. {
  19.         int        clfd;
  20.         pid_t        pid;
  21.        
  22.         for (;;) {
  23.                 if ((clfd = accept(sockfd, NULL, NULL)) < 0) {
  24.                         syslog(LOG_ERR, "accept error: %m\n");
  25.                         exit(-1);
  26.                 }
  27.                 if ((pid = fork()) < 0) {
  28.                         syslog(LOG_ERR, "fork error: %m\n");
  29.                         exit(-1);
  30.                 } else if (pid == 0) {
  31.                         if ((dup2(clfd, STDOUT_FILENO) != STDOUT_FILENO) ||
  32.                             (dup2(clfd, STDERR_FILENO) != STDERR_FILENO)) {
  33.                                 syslog(LOG_ERR, "dup2 error: %m\n");
  34.                                 exit(-1);
  35.                         }
  36.                         close(clfd);
  37.                         execl("/usr/bin/uptime", "uptime", (char *)0);
  38.                 } else {
  39.                         close(clfd);
  40.                         waitpid(pid, NULL, 0);
  41.                 }
  42.         }
  43. }

  44. int
  45. main(void)
  46. {
  47.         struct addrinfo                *ailist, *aip;
  48.         struct addrinfo                hint;
  49.         int                        sockfd;
  50.         int                        err, n;
  51.         char                        *host;
  52.        
  53. #ifdef _SC_HOST_NAME_MAX
  54.         n = sysconf(_SC_HOST_NAME_MAX);
  55.         if (n < 0)
  56. #endif
  57.                 n = HOST_NAME_MAX;
  58.        
  59.         if ((host = malloc(n)) == NULL) {
  60.                 printf("malloc error: %s\n", strerror(errno));
  61.                 exit(-1);
  62.         }
  63.         if (gethostname(host, n) < 0) {
  64.                 printf("gethostname error: %s\n", strerror(errno));
  65.                 exit(-1);
  66.         }
  67.         daemonize("ruptimed");
  68.        
  69.         hint.ai_flags = AI_CANONNAME;
  70.         hint.ai_family = 0;
  71.         hint.ai_socktype = SOCK_STREAM;
  72.         hint.ai_protocol = 0;
  73.         hint.ai_addrlen = 0;
  74.         hint.ai_addr = NULL;
  75.         hint.ai_canonname = NULL;
  76.         hint.ai_next = NULL;
  77.         if ((err = getaddrinfo(host, "ruptimed", &hint, &ailist)) != 0) {
  78.                 syslog(LOG_ERR, "getaddrinfo error: %s", gai_strerror(err));
  79.                 exit(-1);
  80.         }
  81.         for (aip = ailist; aip != NULL; aip = aip->ai_next) {
  82.                 if ((sockfd = initserver(aip->ai_socktype, aip->ai_addr,
  83.                      aip->ai_addrlen, QLEN)) >= 0) {
  84.                         serve(sockfd);
  85.                         exit(0);
  86.                 }
  87.         }
  88.         exit(-1);
  89. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2007-6-20 08:28:27 | 显示全部楼层
应该就是/etc/services
有没有试试这个老一点的函数:getservbyname()
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-6-20 19:47:03 | 显示全部楼层
但是我不关心端口啊,用系统默认分配的就行,关键是getaddrinfo出错,得不到主机地址
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-6-20 22:39:17 | 显示全部楼层
顶一下,请帮忙
回复 支持 反对

使用道具 举报

发表于 2007-6-21 23:50:03 | 显示全部楼层
兄弟,我这里可以啊。
  1.         // daemonize("ruptimed");
  2.         
  3.         hint.ai_flags = AI_CANONNAME;
  4.         hint.ai_family = 0;
  5.         hint.ai_socktype = SOCK_STREAM;
  6.         hint.ai_protocol = 0;
  7.         hint.ai_addrlen = 0;
  8.         hint.ai_addr = NULL;
  9.         hint.ai_canonname = NULL;
  10.         hint.ai_next = NULL;
  11.         if ((err = getaddrinfo(host, "ruptimed", &hint, &ailist)) != 0) {
  12.                 printf("getaddrinfo error: %s\n", gai_strerror(err));
  13.                 exit(-1);
  14.         }
  15. /*      for (aip = ailist; aip != NULL; aip = aip->ai_next) {
  16.                 if ((sockfd = initserver(aip->ai_socktype, aip->ai_addr,
  17.                      aip->ai_addrlen, QLEN)) >= 0) {
  18.                         serve(sockfd);
  19.                         exit(0);
  20.                 }
  21.         }
  22.         */
  23.         printf("OK\n");
复制代码

bumpy:~/tmp$ gcc a.c
bumpy:~/tmp$ ./a.out    <==== 没有改/etc/services 前
getaddrinfo error: Servname not supported for ai_socktype
bumpy:~/tmp$ vi a.c
bumpy:~/tmp$ sudo vi /etc/services  <==== 添加ruptimed   4000/tcp到合适的位置(你的服务里没有别的是4000吧)
bumpy:~/tmp$ ./a.out
OK
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-6-22 04:19:58 | 显示全部楼层
谢谢关注啊,问题解决了,一开始不行是因为上面那个代码里有个笔误,就是pid == 0那个判断条件,我一开始写成fork,汗
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-6-22 12:29:49 | 显示全部楼层

i

真是惭愧啊,本来是问问题的,竟然加精了,其实功劳多半是算roamingo兄弟的,我本来想在程序版再发一篇关于如何使用getsockaddr这个函数的,不过既然这样,我就在这里写吧,如果版主觉得不合适,也可以转到程序版,下面的代码是基于apue第十六章并自己加以修改,以前我以前都是在windows下开发的网络程序,而且中间隔了很长时间,而linux下的c编程又刚接触不久,所以有什么地方理解不到位,希望兄弟们指正。

getsockaddr这个函数的功能是将主机名映射成主机的地址,是个新的接口,以取代以前的gethostbyname这个函数,因为后者不能处理ipv6的地址,并且以被标记为废弃,关于参数的细节有兴趣的朋友可以查帮助,我这里主要说一下应该注意的问题,在使用中,最重要的是hint.ai_flags这个参数的设定问题,服务器端和客户端都分成两种情况,先说服务器端:

1,服务器端以服务的形式提供给用户,相关的代码在3楼,下面的代码是用到的库函数

  1. #include <sys/socket.h>
  2. #include <sys/resource.h>
  3. #include <sys/stat.h>
  4. #include <errno.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <signal.h>
  10. #include <fcntl.h>
  11. #include <syslog.h>

  12. #define MAXSLEEP        128

  13. int
  14. connect_retry(int sockfd, const struct sockaddr *addr, socklen_t alen)
  15. {
  16.         int        nsec;
  17.        
  18.         for (nsec = 1; nsec <= MAXSLEEP; nsec <<= 1) {
  19.                 if (connect(sockfd, addr, alen) == 0)
  20.                         return(0);
  21.                 if (nsec <= MAXSLEEP/2)
  22.                         sleep(nsec);
  23.         }
  24.         return(-1);
  25. }

  26. int
  27. initserver(int type, const struct sockaddr *addr, socklen_t alen, int qlen)
  28. {
  29.         int        fd;
  30.         int        err = 0;
  31.         int        reuse = 1;
  32.        
  33.         if ((fd = socket(addr->sa_family, type, 0)) < 0)
  34.                     return(-1);
  35.         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)) < 0) {
  36.                 err = errno;
  37.                 goto errout;
  38.         }
  39.         if (bind(fd, addr, alen) < 0) {
  40.                 err = errno;
  41.                 goto errout;
  42.         }
  43.         if ((type == SOCK_STREAM) || (type == SOCK_SEQPACKET)) {
  44.                 if (listen(fd, qlen) < 0) {
  45.                         err = errno;
  46.                         goto errout;
  47.                 }
  48.         }
  49.         return(fd);
  50. errout:
  51.         close(fd);
  52.         errno = err;
  53.         return(-1);
  54. }

  55. void
  56. daemonize(const char *cmd)
  57. {
  58.         int                        i, fd0, fd1, fd2;
  59.         pid_t                        pid;
  60.         struct rlimit                rl;
  61.         struct sigaction        sa;
  62.        
  63.         umask(0);
  64.        
  65.         if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
  66.                 printf("getrlimit failed");
  67.                 exit(1);
  68.         }
  69.        
  70.         if ((pid = fork()) < 0) {
  71.                 printf("first fork failed");
  72.                 exit(1);
  73.         } else if (pid > 0) {
  74.                 exit(0);
  75.         }
  76.        
  77.         setsid();
  78.        
  79.         sa.sa_handler = SIG_IGN;
  80.         sigemptyset(&sa.sa_mask);
  81.         sa.sa_flags = 0;
  82.         if (sigaction(SIGHUP, &sa, NULL) < 0) {
  83.                 printf("sigaction failed");
  84.                 exit(1);
  85.         }
  86.         if ((pid = fork()) < 0) {
  87.                 printf("second fork failed");
  88.                 exit(1);
  89.         } else if (pid > 0) {
  90.                 exit(0);
  91.         }
  92.        
  93.         if (chdir("/") < 0) {
  94.                 printf("chdir failed");
  95.                 exit(1);
  96.         }
  97.        
  98.         if (rl.rlim_max == RLIM_INFINITY)
  99.                 rl.rlim_max = 1024;
  100.         for (i = 0; i < rl.rlim_max; i++)
  101.                 close(i);
  102.                
  103.         fd0 = open("/dev/null", O_RDWR);
  104.         fd1 = dup(0);
  105.         fd2 = dup(0);
  106.        
  107.         openlog(cmd, LOG_CONS, LOG_DAEMON);
  108.         if (fd0 != 0 || fd1 != 1 || fd2 != 2) {
  109.                 syslog(LOG_ERR, "unexpected file descriptor %d %d %d", fd0, fd1, fd2);
  110.                 exit(1);
  111.         }
  112. }
复制代码

首先说说什么是服务,端口号大家都知道,比如smtp这个就是服务,而它使用的端口号是25,但是系统怎么知道它需要使用端口25呢,就是通过在/etc/services这个文件里进行登记,打开它你会发现里面登记了几乎所有的像ftp,dns这样的公开的服务,同样的道理,如果我们要使用自己的服务,也需要在里面登记,但要注意端口号不能小于1024,并且不能和已登记的重复,还有一点是关于服务名,这个名字没有必要和程序名一样,比如上面的代码,程序名是ruptimed,而服务名是ruptime,只要你认为能代表程序的功能就行。现在大家应该知道了什么是服务了,就是程序的代号。现在再回到getaddrinfo这个函数,当我将第二个参数设为服务名(ruptime)时,返回的结果里的端口号就是在/etc/services里登记的端口号,有人会说了,那第一个参数呢?是这样的,大家可以打开/etc/hosts这个文件,在开头有几行主机名和地址的列表,下面是我的hosts文件的内容:

  1. 127.0.0.1 localhost
  2. 192.168.1.104 wawxdyy

  3. # The following lines are desirable for IPv6 capable hosts
  4. ::1     ip6-localhost ip6-loopback
  5. fe00::0 ip6-localnet
  6. ff00::0 ip6-mcastprefix
  7. ff02::1 ip6-allnodes
  8. ff02::2 ip6-allrouters
  9. ff02::3 ip6-allhosts
复制代码

如果我将第一个参数设为localhost,那么返回的地址就是127.0.0.1,如果设为wawxdyy,返回的就是192.168.1.104。
如果要想以服务的形式运行服务器程序,这两个参数一定要明确指定,并且主机名不能是localhost,只有这样返回的结果才是有效的,才能用于后面的绑定。
2 服务器程序用端口号的形式发布:这种情况下就没有必要在/etc/services里登记了,但是hint结构的ai_flags的参数设定有注意的地方,在上面的那种情况,因为主机名和服务名都明确提供了,所以即使ai_flags设为0也能返回正确的结果,但是现在我们将第二个参数设为端口号,这样的话,我们必须将hint结构的ai_flags设为AI_PASSIVE,这样返回的结果才能用于后面的监听绑定,否则不能,因为AI_PASSIVE就是告诉getaddrinfo返回的地址是用于监听绑定的。下面是相关的代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <unistd.h>
  6. #include <syslog.h>
  7. #include <netdb.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #include <arpa/inet.h>
  11. #include <sys/wait.h>
  12. #include <fcntl.h>

  13. #ifndef        HOST_NAME_MAX
  14. #define        HOST_NAME_MAX        64
  15. #endif

  16. #define        BUFLEN                128
  17. #define        QLEN                10

  18. extern int initserver(int, const struct sockaddr *, socklen_t, int);
  19. extern void daemonize(const char *);

  20. void
  21. serve(int sockfd)
  22. {
  23.         int        clfd, status;
  24.         pid_t        pid;
  25.        
  26.         for (;;) {
  27.                 if ((clfd = accept(sockfd, NULL, NULL)) < 0) {
  28.                         syslog(LOG_ERR, "accept error: %m\n");
  29.                         exit(-1);
  30.                 }
  31.                
  32.                 if ((pid = fork()) < 0) {
  33.                         syslog(LOG_ERR, "fork error: %m\n");
  34.                         exit(-1);
  35.                 } else if (pid == 0) {
  36.                         if (dup2(clfd, STDOUT_FILENO) != STDOUT_FILENO ||
  37.                             dup2(clfd, STDERR_FILENO) != STDERR_FILENO) {
  38.                                 syslog(LOG_ERR, "dup2 error: %m\n");
  39.                                 exit(-1);
  40.                         }
  41.                         close(clfd);
  42.                         execl("/usr/bin/uptime", "uptime", (char *)0);
  43.                         syslog(LOG_ERR, "unexpected return from execl: %m");
  44.                 } else {
  45.                         close(clfd);
  46.                         waitpid(pid, &status, 0);
  47.                 }
  48.         }
  49. }

  50. int
  51. main(void)
  52. {
  53.         struct addrinfo                *ailist, *aip;
  54.         struct addrinfo                hint;
  55.         struct sockaddr_in        *sinp;
  56.         int                        sockfd;
  57.         int                        err, n;
  58.         char                        *host;
  59.         char                        buf[INET_ADDRSTRLEN];
  60.        
  61. #ifdef _SC_HOST_NAME_MAX
  62.         n = sysconf(_SC_HOST_NAME_MAX);
  63.         if (n < 0)
  64. #endif
  65.                 n = HOST_NAME_MAX;
  66.        
  67.         if ((host = malloc(n)) == NULL) {
  68.                 printf("malloc error: %s\n", strerror(errno));
  69.                 exit(-1);
  70.         }
  71.         if (gethostname(host, n) < 0) {
  72.                 printf("gethostname error: %s\n", strerror(errno));
  73.                 exit(-1);
  74.         }
  75.         syslog(LOG_ERR, "hostname is %s", host);
  76.         daemonize("ruptimed");
  77.        
  78.         hint.ai_flags = AI_PASSIVE;
  79.         hint.ai_family = 0;
  80.         hint.ai_socktype = SOCK_STREAM;
  81.         hint.ai_protocol = 0;
  82.         hint.ai_addrlen = 0;
  83.         hint.ai_addr = NULL;
  84.         hint.ai_canonname = NULL;
  85.         hint.ai_next = NULL;
  86.         if ((err = getaddrinfo(NULL, "2000", &hint, &ailist)) != 0) {
  87.                 syslog(LOG_ERR, "getaddrinfo error: %s", gai_strerror(err));
  88.                 exit(-1);
  89.         }
  90.         for (aip = ailist; aip != NULL; aip = aip->ai_next) {
  91.                 sinp = (struct sockaddr_in *)aip->ai_addr;
  92.                 short port = ntohs(sinp->sin_port);
  93.                 syslog(LOG_ERR, "port is %d\n", port);
  94.                 if (inet_ntop(aip->ai_family, &sinp->sin_addr, buf, INET_ADDRSTRLEN) != NULL)
  95.                         syslog(LOG_ERR, "addr is %s\n", buf);
  96.                 if ((sockfd = initserver(aip->ai_socktype, aip->ai_addr,
  97.                      aip->ai_addrlen, QLEN)) >= 0) {
  98.                         serve(sockfd);
  99.                         exit(0);
  100.                 }
  101.         }
  102.         exit(-1);
  103. }
复制代码

再来说说客户端:相似地也有两种情况:
1 以服务的方式连接服务器:如果用户只知道服务器提供的服务,而不知道其端口号,就用这种方式,并且我们也需要在/etc/services里登记,否则也会出现“Servname not supported for ai_socktype”这样的错误,但是端口号不必于服务器端一样,下面是代码:

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <netdb.h>
  7. #include <sys/socket.h>
  8. #include <arpa/inet.h>
  9. #include <netinet/in.h>

  10. #define BUFLEN                128

  11. extern connect_retry(int sockfd, const struct sockaddr *addr, socklen_t alen);

  12. int
  13. print_uptime(int sockfd)
  14. {
  15.         int        n;
  16.         char        buf[BUFLEN];
  17.        
  18.         while ((n= recv(sockfd, buf, BUFLEN, 0)) > 0) {
  19.                 if (write(STDOUT_FILENO, buf, n) != n)
  20.                         return(-1);
  21.         }
  22.         if (n < 0)
  23.                 return(-1);
  24.         if (n == 0)
  25.                 printf("nothing received\n");
  26.         return(0);
  27. }

  28. int
  29. main(int argc, char *argv[])
  30. {
  31.         struct addrinfo                *ailist, *aip;
  32.         struct addrinfo                hint;
  33.         struct sockaddr_in        *sinp;
  34.         int                        sockfd;
  35.         int                        err;
  36.         char                        seraddr[INET_ADDRSTRLEN];
  37.         short                        serport;
  38.        
  39.         if (argc != 2) {
  40.                 printf("usage: %s <hostname>\n", argv[0]);
  41.                 exit(-1);
  42.         }
  43.        
  44.         hint.ai_family = 0;
  45.         hint.ai_socktype = SOCK_STREAM;
  46.         hint.ai_flags = AI_CANONNAME;
  47.         hint.ai_protocol = 0;
  48.         hint.ai_addrlen = 0;
  49.         hint.ai_addr = NULL;
  50.         hint.ai_canonname = NULL;
  51.         hint.ai_next = NULL;
  52.         if ((err = getaddrinfo(argv[1], "ruptime", &hint, &ailist)) != 0) {
  53.                 printf("getaddrinfo error: %s\n", gai_strerror(err));
  54.                 exit(-1);
  55.         }
  56.         printf("getaddrinfo ok\n");
  57.         for (aip = ailist; aip != NULL; aip = aip->ai_next) {
  58.                
  59.                 sinp = (struct sockaddr_in *)aip->ai_addr;
  60.                 if (inet_ntop(sinp->sin_family, &sinp->sin_addr, seraddr, INET_ADDRSTRLEN) != NULL)
  61.                 {
  62.                         printf("server address is %s\n", seraddr);
  63.                 }
  64.                 serport = ntohs(sinp->sin_port);
  65.                 printf("server port is %d\n", serport);
  66.                
  67.                 if ((sockfd = socket(aip->ai_family, SOCK_STREAM, 0)) < 0) {
  68.                         printf("create socket failed: %s\n", strerror(errno));
  69.                         exit(-1);
  70.                 }printf("create socket ok\n");
  71.                 if (connect_retry(sockfd, aip->ai_addr, aip->ai_addrlen) < 0) {
  72.                         printf("can't connect to %s: %s\n", argv[1], strerror(errno));
  73.                         exit(-1);
  74.                 } else {
  75.                         printf("connect ok\n");
  76.                         if (print_uptime(sockfd) < 0) {
  77.                                 printf("print_uptime error: %s\n", strerror(errno));
  78.                                 exit(-1);
  79.                         }
  80.                 }
  81.         }
  82.         exit(0);
  83. }
复制代码

2 以端口号的方式连接服务器:这个就有一点需要注意,hint的ai_flags不要设为AI_PASSIVE,因为返回的地址不是用来监听绑定的,一般设为AI_CANONNAME,代码就不贴了,照这上面的改改就可以了。

我自认为不太会写东西,并且由于种种原因,已经24小时没睡了,脑袋有点晕,再加上水平有限,所以有错误的地方兄弟们请指出来,另外有看不太明白的地方就跟帖,最后谢谢版主给我这个精,大家互相学习。
回复 支持 反对

使用道具 举报

发表于 2007-6-22 16:40:02 | 显示全部楼层
我毫不犹豫地收藏了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表