最新消息:

Linux Socket 学习笔记23-守护进程inetd

C/C++ admin 2874浏览 0评论

守护进程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
<span style="color: #808080; font-style: italic;">/* inetd_test.c:
* Example inetd daytime server :
*/</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;unistd.h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;errno.h&gt;</span>
<span style="color: #339933;">#include &lt;string.h&gt;</span>
<span style="color: #339933;">#include &lt;time.h&gt;</span>
<span style="color: #339933;">#include &lt;sys/types.h&gt;</span>
<span style="color: #808080; font-style: italic;">/*
* This function reports the error and
* exits back to the shell :
*/</span>
<span style="color: #993333;">static</span> <span style="color: #993333;">void</span>
bail<span style="color: #009900;">(</span><span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>on_what<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span> errno <span style="color: #339933;">!=</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
fputs<span style="color: #009900;">(</span>strerror<span style="color: #009900;">(</span>errno<span style="color: #009900;">)</span><span style="color: #339933;">,</span>stderr<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
fputs<span style="color: #009900;">(</span><span style="color: #ff0000;">": "</span><span style="color: #339933;">,</span>stderr<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
fputs<span style="color: #009900;">(</span>on_what<span style="color: #339933;">,</span>stderr<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
fputc<span style="color: #009900;">(</span><span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">n</span>'</span><span style="color: #339933;">,</span>stderr<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
exit<span style="color: #009900;">(</span><span style="color: #0000dd;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>

<span style="color: #993333;">int</span>
main<span style="color: #009900;">(</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span><span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<span style="color: #993333;">int</span> z<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> n<span style="color: #339933;">;</span>
time_t td<span style="color: #339933;">;</span>        <span style="color: #808080; font-style: italic;">/* Current date&amp;amp;time */</span>
<span style="color: #993333;">char</span> dtbuf<span style="color: #009900;">[</span><span style="color: #0000dd;">128</span><span style="color: #009900;">]</span><span style="color: #339933;">;</span>     <span style="color: #808080; font-style: italic;">/* Date/Time info */</span>

<span style="color: #808080; font-style: italic;">/*
* Generate a time stamp :
*/</span>
time<span style="color: #009900;">(</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>td<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
n <span style="color: #339933;">=</span> <span style="color: #009900;">(</span><span style="color: #993333;">int</span><span style="color: #009900;">)</span> strftime<span style="color: #009900;">(</span>dtbuf<span style="color: #339933;">,</span><span style="color: #993333;">sizeof</span> dtbuf<span style="color: #339933;">,</span>
<span style="color: #ff0000;">"%A %b %d %H:%M:%S %Y<span style="color: #000099; font-weight: bold;">n</span>"</span><span style="color: #339933;">,</span>
localtime<span style="color: #009900;">(</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>td<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>

<span style="color: #808080; font-style: italic;">/*
* Write result back to the client :
*/</span>
z <span style="color: #339933;">=</span> write<span style="color: #009900;">(</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span>dtbuf<span style="color: #339933;">,</span>n<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span> z <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span> <span style="color: #009900;">)</span>
bail<span style="color: #009900;">(</span><span style="color: #ff0000;">"write(2)"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>

<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>

代码确实简单多了。

然后编译:

1
$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> inetd_test.c <span style="color: #660033;">-o</span> inetd_test

现在就可以通过inet调用它了。

为了方便,首先将可执行文件复制到/tmp目录下:

1
$ <span style="color: #c20cb9; font-weight: bold;">cp</span> inetd_test <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>inetd_test

再给它加是执行权限:

1
$ <span style="color: #c20cb9; font-weight: bold;">chmod</span> a+rx <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>inetd_test

然后修改inet的配置文件inetd.conf:

1
2
3
$ <span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-l</span> <span style="color: #000000; font-weight: bold;">/</span>tec<span style="color: #000000; font-weight: bold;">/</span>inetd.conf

<span style="color: #000000;">9999</span> stream tcp nowait root <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>inet_test null

最后重启inetd进程:

查找inetd进程的pid:

1
$ <span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-ax</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> inetd

例如pid为333,重启:

1
$ <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-HUP</span> <span style="color: #000000;">333</span>

转载请注明:爱开源 » Linux Socket 学习笔记23-守护进程inetd

您必须 登录 才能发表评论!