⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fig16.15

📁 unix环境高级编程第二版配套源代码 unix环境高级编程第二版配套源代码
💻 15
字号:
#include "apue.h"#include <netdb.h>#include <errno.h>#include <syslog.h>#include <sys/socket.h>#define BUFLEN	128#define QLEN 10#ifndef HOST_NAME_MAX#define HOST_NAME_MAX 256#endifextern int initserver(int, struct sockaddr *, socklen_t, int);voidserve(int sockfd){	int		clfd;	FILE	*fp;	char	buf[BUFLEN];	for (;;) {		clfd = accept(sockfd, NULL, NULL);		if (clfd < 0) {			syslog(LOG_ERR, "ruptimed: 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);	}}intmain(int argc, char *argv[]){	struct addrinfo	*ailist, *aip;	struct addrinfo	hint;	int				sockfd, err, n;	char			*host;	if (argc != 1)		err_quit("usage: ruptimed");#ifdef _SC_HOST_NAME_MAX	n = sysconf(_SC_HOST_NAME_MAX);	if (n < 0)	/* best guess */#endif		n = HOST_NAME_MAX;	host = malloc(n);	if (host == NULL)		err_sys("malloc error");	if (gethostname(host, n) < 0)		err_sys("gethostname error");	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, "ruptimed: getaddrinfo error: %s",		  gai_strerror(err));		exit(1);	}	for (aip = ailist; aip != NULL; aip = aip->ai_next) {		if ((sockfd = initserver(SOCK_STREAM, aip->ai_addr,		  aip->ai_addrlen, QLEN)) >= 0) {			serve(sockfd);			exit(0);		}	}	exit(1);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -