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