守护进程inetd的一个优点就是可以简化服务程序代码的编写。例如对TCP服务程序
序,无需反复编写socket,bind,listen,accept函数调用。
下面来一个简单的实例。首先,编写代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | /* inetd_test.c: * Example inetd daytime server : */ #include |
代码确实简单多了。
然后编译:
1 | $ gcc inetd_test.c -o inetd_test |
现在就可以通过inet调用它了。
为了方便,首先将可执行文件复制到/tmp目录下:
1 | $ cp inetd_test /tmp/inetd_test |
再给它加是执行权限:
1 | $ chmod a+rx /tmp/inetd_test |
然后修改inet的配置文件inetd.conf:
1 2 3 | $ tail -l /tec/inetd.conf 9999 stream tcp nowait root /tmp/inet_test null |
最后重启inetd进程:
查找inetd进程的pid:
1 | $ ps -ax | grep inetd |
例如pid为333,重启:
1 | $ kill -HUP 333 |