1.Xinetd提供被動式的超級服務,也就是服務程式是被使用端所啟動,平時則無須存在。例如,ftp, telnetd, pop3,imap, auth等等,這些服務沒有人使用時,無須啟動。此外,xinetd將socket轉換成stdin/stdout,因而使得網路服務程式設計大大簡化,您可以只用printf及fgets便可完成處理很複雜的網路協定。
2.一個簡單的服務程式 goodie
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
char *cmds[]={
"help",
"say",
"hello",
"bye",
"exit",
NULL
};
int getcmd(char *cmd)
{
int n=0;
while (cmds[n]!=NULL) {
if (strncasecmp(cmd,cmds[n],strlen(cmds[n]))==0) return n;
n++;
}
return -1;
}
void main(void)
{
char buf[1024];
int ok;
printf("Welcome to goodie service! ");
fflush(stdout);
ok=0;
do {
while (fgets(buf,1023,stdin)==NULL);
switch (getcmd(buf)) {
case -1: printf("Unknown command! "); break;
case 0:
printf("How may I help you, sir? ");
int j=0;
while(cmds[j++]!=NULL)printf(” %s\t\t“,cmds[j-1]);
break;
case 1: printf("I will say %s",&buf[3]); break;
case 2: printf("How're you doing today? "); break;
case 3: printf("Si ya, mate! "); ok=1; break;
case 4: printf("Go ahead! "); ok=1; break;
}
fflush(stdout);
} while (!ok);
}