获取本机IP地址


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

int main(int argc, char* argv[])
{
	WSADATA wsadata;
	char	szHostName[128] = {0x00};
	char	szHostIpAdd[128] = {0x00};
	
	int		iRet = 0;
	if (WSAStartup(MAKEWORD(2,1), &wsadata))
	{
		printf("Winsock 无法初始化。。。");
		WSACleanup();
		return 0;
	}	
	
	iRet = gethostname(szHostName, 128);
	if (iRet != 0)
	{
		printf("Get HostName Error %d", iRet);
	}

	struct hostent *pstHostInfo = gethostbyname(szHostName);
	
	struct in_addr addr;
	memcpy(&addr, pstHostInfo->h_addr_list[0], sizeof(struct in_addr));
	strcpy(szHostIpAdd, inet_ntoa(addr));
	printf(szHostIpAdd);
	printf("\n");

	return 0;
}





你可能感兴趣的:(Socket套接字,struct,include,list)