xinetd编程

下面一个例子简单的描述了xinet编程的步骤:

1. 我写了一个小程序:
/**
*** tcpgoodie.c
**/
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h>

main(void) 

 printf("Welcome to goodie service!
"); 
}

2. 编译
gcc -o tcpgoodie tcpgoodie.c

3. 修改了 /etc/services
我增加了一行:
tcpgoodie 20001/tcp #目的是分配20001端口给tcpgoodie,这个端口和别的端口也不冲突

4. 修改了 /etc/xinetd.conf
我在 includedir /etc/xinetd.d 下面增加了:

service tcpgoodie
{
    socket_type = stream
    protocol = tcp
    wait = no
    user = root
    server = /usr/sbin/tcpgoodie
    port = 20001
}

5. 重启xinetd
/etc/rc.d/init.d/xinetd restart
或者:
service xinetd restart

6. 在windows下面运行cmd,输入:telnet 192.168.0.40 20001
192.168.0.40是我linux机器的IP
得到的结果是:
Trying 192.168.0.40...
Connected to 192.168.0.40.
Escape character is ’^]’.
Welcome to goodie service!
Connection closed by foreign host.

注意1:
inetd编程和xinetd编程的语法是不同的。
我的环境是linux redhat9,在/etc/下面只有xinetd.conf,而没有inetd.conf
如果是inetd,那么语法是:
<service_name> <sock_type> <proto> <flag> <user> <server_path> <args>

注意2:
在第5步中,key 和 = 以及value 和 = 之间必须要有空格或者Tab键,否则不行。比如wait = no,wait和=之间有空格,如果这样写:wait=no,那就不行。
也可以不修改xinetd.conf,而在/etc/xinetd.d/ 建立一个文件 tcpgoodie, 然后 vi tcpgoodie,把:
service tcpgoodie
{
    socket_type = stream
    protocol = tcp
    wait = no
    user = root
    server = /usr/sbin/tcpgoodie
    port = 20001
}
写在里面, 继续下面的步骤。

轉自:http://atu82.bokee.com/5098677.html

________________________________________________________________________

基於xinetd的echo服務器:

#include <stdio.h>
#include
<stdlib.h>
#include
<string.h> // bzero
#include <unistd.h>
#include
<sys/socket.h>
#include
<netinet/in.h>
#include
<arpa/inet.h> // inet_ntop

//=============================================================
// 语法格式: void main(void)
// 实现功能: 主函数,建立一个TCP Echo Server
// 入口参数: 无
// 出口参数: 无
//=============================================================

int main(int argc, char *argv[])
{
int nread;
char buff[256];

if( (nread = read(0,buff,256)) > 0)
{
buff[nread]
= 0;
write(
1,buff,nread);
}
return 0;
}

  

services,xinetd.conf文件的配置同上.

原文地址:https://www.cnblogs.com/hnrainll/p/2168868.html