為了方便操作,linux提供了一組用于查詢協(xié)議的值及名稱的函數(shù)。
xxxprotoxxx()函數(shù):
上面的函數(shù)對文件/etc/protocols中的記錄進(jìn)行操作,文件中記錄了協(xié)議的名稱、值和別名等值,與struct protoent的定義一致。
使用協(xié)議族函數(shù)的例子:
首先,使用setprotoent(1)打開文件/etc/protocols,然后使用函數(shù)getprotobyname()查詢函數(shù)并顯示出來,最后使用函數(shù)endprotoent()關(guān)閉文件/etc/protocols。
#include <netdb.h>#include <stdio.h>voiddisplay_protocol(struct protoent *pt){ int i = 0; if(pt) { printf("protocol name: %s", pt->p_name); if(pt->p_aliases) { printf("alias name:"); while(pt->p_aliases[i]) { printf("%s ", pt->p_aliases[i]); i++; } } printf(", value: %d/n", pt->p_proto); }}intmain(int argc, char **argv){ int i = 0; const char *const protocol_name[] = { "ip", "icmp", "tcp", "udp", NULL }; setprotoent(1); while(protocol_name[i] != NULL) { struct protoent *pt = getprotobyname((const char *)&protocol_name[i][0]); if(pt) { display_protocol(pt); } i++; } endprotoent(); return(0);}
運(yùn)行結(jié)果:
|
新聞熱點(diǎn)
疑難解答
圖片精選