1 #include <stdio.h> 2 #include <unistd.h> 3 #include <netdb.h> 4 5 int main(int argc,char *argv[]) 6 { 7 struct servent serv; 8 struct servent *serv_info=&serv; 9 10 serv_info=getservbyport(htons(atoi(argv[1])),NULL);/*第二个参数为协议类型,为NULL时自动匹配所有协议*/ 11 printf("%s\n%d\n",serv_info->s_name,ntohs(serv_info->s_port)); 12 serv_info=getservbyport(htons(atoi(argv[1])),"tcp"); 13 printf("%s\n%d\n",serv_info->s_name,ntohs(serv_info->s_port)); 14 15 return 0; 16 17 }