【linux编程】gethostname、gethostbyname 函数解析

gethostname、gethostbyname 函数解析

 

#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>

void handler(int sig)
{
	printf("recv a sig=%d\n", sig);
	exit(EXIT_SUCCESS);
}

#define ERR_EXIT(m)     \
	do {                   \
		perror(m);          \
		exit(EXIT_FAILURE); \
	} while (0);

int main(void)
{
	char host[100] = {0};
	if (gethostname(host, sizeof(host)) < 0) {
		ERR_EXIT("gethostname");
	}
	struct hostent *hp;
	if ((hp = gethostbyname(host)) == NULL) {
		ERR_EXIT("gethostbyname");
	}

	int i = 0;
	while (hp->h_addr_list[i] != NULL) {
		printf("hostname: %s\n", hp->h_name);
		printf("ip: %s\n", inet_ntoa(*(struct in_addr *)hp->h_addr_list[i]));
		i++;
	}
	return 0;
}

 

参考资料

1. 使用gethostname()函数和gethostbyname()函数获取主机相关信息

posted @ 2022-12-07 16:08  苏格拉底的落泪  阅读(193)  评论(0编辑  收藏  举报