//获取本机地址列表之Unicode版本
//Author:zerosoul
#include "stdafx.h"
#include "winsock2.h"
#include "tchar.h"
#pragma comment(lib, "Ws2_32.lib")
int _tmain(int argc, TCHAR* argv[])
{
WSADATA WSAData;
if (WSAStartup(MAKEWORD(2,2), &WSAData)!=0)
{
_tprintf(_T("WSAStartup Error:[%d]"),WSAGetLastError());
}
DWORD dwIP = 0;
int i = 0;
struct hostent* lpHst = NULL;
char HostName[256] = {0};
gethostname(HostName,256);
lpHst = gethostbyname(HostName);
while( lpHst->h_addr_list[i] != NULL )
{
dwIP = *(DWORD*)lpHst->h_addr_list[i++];
SOCKADDR_IN Addr = {0};
Addr.sin_family = AF_INET; //这个必须设置,否则WSAAddressToString()会报错
Addr.sin_addr.s_addr = dwIP;
TCHAR strAddr[32] = {0};
DWORD addrLen = 32;
//关键是下面这句,没有ANSI版本的inet_ntoa()那么简单
if (SOCKET_ERROR == WSAAddressToString((struct sockaddr *)&Addr,sizeof(Addr),NULL,strAddr,&addrLen))
{
_tprintf(_T("WSAAddressToString() Error:[%d]\n"),WSAGetLastError());
}
_tprintf(_T("IPAddr%d : %s\n"),i,strAddr);
}
return 0;
}
获取本机地址列表之Unicode版本
最新推荐文章于 2021-09-17 02:59:19 发布