getservbyport()函数用法!
时间:2006-09-23 来源:skai
/* getservbyport() function */
#include <stdlib.h>
#include <netdb.h>
#include <stdio.h>
void err_sys(const char *p_error);
int
main(int argc, char **argv)
{
int i;
struct servent *sptr;
if ((sptr = getservbyport(htons(atoi(argv[1])), "tcp")) == NULL)
err_sys("getservbyport error");
printf("service name: %s\n", sptr->s_name);
for (i = 0; sptr->s_aliases[i] != NULL; i++)
printf("aliases: %s\n", sptr->s_aliases[i]);
printf("port = %d\n", ntohs(sptr->s_port));
printf("protocol = %s\n", sptr->s_proto);
exit(0);
}
void
err_sys(const char *p_error)
{
perror(p_error);
exit(1);
}
/* note: 注意传递给getservbyport()函数的第1个参数必须是网络字节序,
* 函数返回时注意sptr->s_port成员是网络字节序,所以必须转换
* 成主机字节序后再输出.
*/
#include <stdlib.h>
#include <netdb.h>
#include <stdio.h>
void err_sys(const char *p_error);
int
main(int argc, char **argv)
{
int i;
struct servent *sptr;
if ((sptr = getservbyport(htons(atoi(argv[1])), "tcp")) == NULL)
err_sys("getservbyport error");
printf("service name: %s\n", sptr->s_name);
for (i = 0; sptr->s_aliases[i] != NULL; i++)
printf("aliases: %s\n", sptr->s_aliases[i]);
printf("port = %d\n", ntohs(sptr->s_port));
printf("protocol = %s\n", sptr->s_proto);
exit(0);
}
void
err_sys(const char *p_error)
{
perror(p_error);
exit(1);
}
/* note: 注意传递给getservbyport()函数的第1个参数必须是网络字节序,
* 函数返回时注意sptr->s_port成员是网络字节序,所以必须转换
* 成主机字节序后再输出.
*/
相关阅读 更多 +