【程序】Windows下根据计算机名获取IP地址的C语言程序

#include <stdio.h>
#include <WinSock2.h>

#pragma comment(lib, "ws2_32.lib")

// 根据计算机名获取IP地址
LPIN_ADDR get_computer_ip(const char *name)
{
	struct hostent *ent = gethostbyname(name);
	if (ent != NULL)
		return (LPIN_ADDR)ent->h_addr_list[0];
	else
		return NULL;
}

int main(void)
{
	LPIN_ADDR addr;
	WSADATA wsaData;
	WSAStartup(MAKEWORD(2, 2), &wsaData);
	
	addr = get_computer_ip("STM32F103RE");
	if (addr)
		printf("IP: %s\n", inet_ntoa(*addr));
	else
		printf("No IP!\n");

	WSACleanup();
	return 0;
}

程序运行结果:


猜你喜欢

转载自blog.csdn.net/zlk1214/article/details/79595245
今日推荐