【发布时间】:2014-11-23 17:36:27
【问题描述】:
我刚开始学习 C 编程,为了练习,我找到了这个任务。首先,我必须以协议的名义进行扫描。然后我必须检查协议的正式名称、编号和别名。因此,如果我输入 tcp,输出将如下所示:
正式名称:tcp
原型编号:6
别名 TCP
这是我到目前为止所得到的。当我运行它并输入 ip 或 tcp 时,它不会给我任何错误。但它说没有找到协议。
提前感谢您的帮助。
#include <netdb.h>
#include <stdio.h>
int main(){
char name[200];
int i;
struct protoent *proto = getprotobyname(name);
printf("Enter protocol name: ");
scanf("%c", name);
proto = getprotobyname( name );
if ( proto != NULL )
{
printf("official name: %s\n", proto->p_name);
printf("proto number: %d\n", proto->p_proto);
for ( i = 0; proto->p_aliases[i] != 0; i++ ){
printf("alias: %s\n", proto->p_aliases[i]);
}
}
else{
perror("protocol not found");
}
return 0;
}
【问题讨论】:
-
Bug 1:
getprotobyname(name);被调用了两次,一次在name初始化之前,第二次在它之后。Bug 2:scanf("%c", name);必须是scanf("%s", name);
标签: c network-programming