protoent
protoent
protoent 结构体描述:
/* Description of data base entry for a single service. */ struct protoent { char *p_name; /* Official protocol name. */ char **p_aliases; /* Alias list. */ int p_proto; /* Protocol number. */ };
相关函数:
/* Open protocol data base files and mark them as staying open even after a later search if STAY_OPEN is non-zero. This function is a possible cancellation point and therefore not marked with __THROW. */ extern void setprotoent (int __stay_open); /* Close protocol data base files and clear `stay open' flag. This function is a possible cancellation point and therefore not marked with __THROW. */ extern void endprotoent (void); /* Get next entry from protocol data base file. Open data base if necessary. This function is a possible cancellation point and therefore not marked with __THROW. */ extern struct protoent *getprotoent (void); /* Return entry from protocol data base for network with NAME. This function is a possible cancellation point and therefore not marked with __THROW. */ extern struct protoent *getprotobyname (__const char *__name); /* Return entry from protocol data base which number is PROTO. This function is a possible cancellation point and therefore not marked with __THROW. */ extern struct protoent *getprotobynumber (int __proto);
测试代码:
1 #include <stdio.h> 2 #include <netdb.h> 3 4 int main() 5 { 6 int stayopen = 1; 7 struct protoent *proto; 8 char **ptr; 9 10 /* 打开协议数据库 */ 11 setprotoent(stayopen); 12 13 /* 逐项扫描登记项 */ 14 while(1) 15 { 16 proto = getprotoent(); 17 18 if( proto != (struct protoent *)0 ) 19 { 20 printf("Official name : %s\n", proto->p_name); 21 22 for(ptr=proto->p_aliases; *ptr != 0; ptr++) 23 printf("alias: %s\n", *ptr); 24 25 printf("proto: %d\n\n", proto->p_proto); 26 } 27 else 28 break; 29 } 30 31 /* 关闭协议数据库 */ 32 endprotoent(); 33 34 /* 通过名字察看协议 */ 35 printf("getprotobyname [ip]: \n"); 36 if( 0 != (proto = getprotobyname("ip")) ) 37 { 38 for(ptr=proto->p_aliases; *ptr != 0; ptr++) 39 printf("alias: %s\n", *ptr); 40 } 41 else 42 printf("failure ...\n"); 43 44 /* 通过协议号(按主机字节顺序)察看协议 */ 45 printf("getprotobynumber [1]: \n"); 46 if( 0 != (proto = getprotobynumber(1)) ) 47 { 48 printf("name: %s\n", proto->p_name); 49 } 50 else 51 printf("failue ...\n"); 52 53 return 0; 54 }
运行截图: