环境:redhat linux 9
参考:
1.《UNIX网络编程》(第一卷), 第12章 守护进程和inetd超级服务器
2.http://www.cublog.cn/u/5774/showart.php?id=150322
配置步骤如下:
1、在/etc/xinetd.d目录下建立文件mydaytime并编辑其内容如下:
service mydaytime
{
disable = no
type = UNLISTED
socket_type = stream
protocol = tcp
wait = no
server = /home/dazhao/unpv12e/inetd/daytimetcpsrv3
port = 9999
}
可能你已经注意到了/home/dazhao/unpv12e/inetd/daytimetcpsrv3文件,观其路径名,以您的聪明才智,肯定知道它是从哪来的,呵呵。
daytimetcpsrv3.c代码如下:
#include "unp.h"
#include <time.h>
int
main(int argc, char **argv)
{
socklen_t len;
struct sockaddr *cliaddr;
char buff[MAXLINE];
time_t ticks;
daemon_inetd(argv[0], 0);
cliaddr = Malloc(MAXSOCKADDR);
len = MAXSOCKADDR;
Getpeername(0, cliaddr, &len);
err_msg("connection from %s", Sock_ntop(cliaddr, len));
ticks = time(NULL);
snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
Write(0, buff, strlen(buff));
Close(0); /* close TCP connection */
exit(0);
}
~
|
2、重新启动xinetd服务
# service xinetd restart
执行netstat检验是否已创建了一个在TCP的端口9999上的监听套接口:
# netstat -na | grep 9999
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN
3、从另外一台主机上访问这个服务,假设宿主机ip是192.168.8.129:
# telnet 192.168.8.129 9999
Trying 192.168.8.129...
onnected to 192.168.8.129 (192.168.8.129).
Escape character is '^]'.
Thu Mar 29 20:59:35 2007
Connection closed by foreign host.
OK,试验成功,it looks very simple~, but first u must know how does XINETD work.
阅读(1681) | 评论(0) | 转发(0) |