ie service。
现在我们来检验一下goodie是否可以执行:
telnet localhost 20001
或
telnet your_host_name 20001
执行结果
Trying 127.0.0.1...
Connected to localhost.
Escape character is ''''^]''''.
Welcome to goodie service!
Connection closed by foreign host.
很简单不是吗? 信不信由您,telnet/pop3/imap/ftp都是靠这种方式建立起来
的服务。
我们现在来建立一点小小的"网路协定",这个协定使我们可以输入"exit"时,
离开程式,而其他的指令都是输出与输入相同的字串。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(void)
{
char buf[1024];
int ok;
printf("Welcome to goodie service!\n");
fflush(stdout);
ok=0;
do {
while (fgets(buf,1023,stdin)==NULL);
if (strncasecmp(buf,"exit",4)==0) ok=1;
printf(buf);
fflush(stdout);
} while (!ok);
}
执行结果
telnet localhost 20001
或
telnet your_host_name 20001
Trying 127.0.0.1...
Connected to localhost.
Escape character is ''''^]''''.
Welcome to goodie service!
输入"help"
help
help
输入"exit"
exit
exit
Connection closed by foreign host.
接下来,我们将设计一个稍微复杂一点点的通讯协定,比较通用於一般用途。
#include <stdio.h>
#include <stdlib.h>
#include <string.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!\n");
fflush(stdout);
ok=0;
do {
while (fgets(buf,1023,stdin)==NULL);
switch (getcmd(buf)) {
case -1: printf("Unknown command!\n"); break;
case 0: printf("How may I help you, sir?\n"); break;
case 1: printf("I will say %s",&buf[3]); break;
case 2: printf("How''''re you doing today?\n"); break;
case 3: printf("Si ya, mate!\n"); ok=1; break;
case 4: printf("Go ahead!\n"); ok=1; break;
}
fflush(stdout);
} while (!ok);
}
telnet localhost 20001
或
telnet your_host_name 20001
试试看输入"help"、"say"、"hello"、"bye"、"exit"等等指令,及其它一些不
在命令列中的指令。
在设计inetd服务程式时,要特别注意buffer overflow的问题,也就是以下这
种状况:
char buffer_overflow[64];
fscanf(stdin,"%s",buffer_overflow);
历来几乎所有的安全漏洞都是由此而来的。
你一定不可这样用,不论任何理由,类同的用法也不可以。Cracker可以透过将
您的buffer塞爆,然後塞进他自己的程式进来执行。
OK STATION, Webmaster, Brian Lin
上一页 [1] [2] [3] [VB.NET程序]几个 WMI 的例子(初级) - 1 [VB.NET程序]几个 WMI 的例子(初级) - 2 [VB.NET程序]几个 WMI 的例子(初级) - 3 [VB.NET程序]几个 WMI 的例子(初级) - 4 [Delphi程序][Tips]挂起 - 运行外部程式,外部程式退出 - 继续… [Delphi程序]升级到Delphi 6 - 兼容性问题(中文全文) [Delphi程序]升级到Delphi 6 - 兼容性问题之三(完) [Delphi程序]升级到Delphi 6 - 兼容性问题之二 [Delphi程序]升级到Delphi 6 - 兼容性问题之一 [Delphi程序]2001 年軟體界的巨星 - Kylix:李维.
|