获取ip地址失败,我认为是字符编码的原因,有时间的帮忙看看啊

0c0c0f 2013-03-22 03:20:04

WSADATA wsadata;
if(0!=WSAStartup(MAKEWORD(2,2),&wsadata))//初始化
{
AfxMessageBox(_T("初始化网络环境失败!"));
return true;
}
char name[255];
gethostname(name,sizeof(name));
hostent *p=gethostbyname(name);
if(p!=NULL)
{
DWORD dwNum = MultiByteToWideChar(CP_ACP,0,(char *)p->h_addr_list,-1,NULL,0);
wchar_t *pwAddr;
pwAddr = new wchar_t[dwNum];
pwAddr=(wchar_t*)p->h_addr_list;
m_Addr.SetWindowTextW(pwAddr);
if(!pwAddr)
{
delete []pwAddr;
}
}
...全文
69 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jdzfjfhnui 2013-03-22
  • 打赏
  • 举报
回复
typedef struct hostent { char FAR *h_name; char FAR FAR **h_aliases; short h_addrtype; short h_length; char FAR FAR **h_addr_list; } HOSTENT, *PHOSTENT, FAR *LPHOSTENT; h_addr_list : A NULL-terminated list of addresses for the host. Addresses are returned in network byte order. The macro h_addr is defined to be h_addr_list[0] for compatibility with older software. 是指向 DWORD 数组,每个item是一个地址,最后一个item是0,可以通过inet_ntoa转成字符串形式 #define WIN32_LEAN_AND_MEAN #include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h> // Need to link with Ws2_32.lib #pragma comment(lib, "ws2_32.lib") int main(int argc, char **argv) { //----------------------------------------- // Declare and initialize variables WSADATA wsaData; int iResult; DWORD dwError; int i = 0; struct hostent *remoteHost; char *host_name; struct in_addr addr; char **pAlias; // Validate the parameters if (argc != 2) { printf("usage: %s ipv4address\n", argv[0]); printf(" or\n"); printf(" %s hostname\n", argv[0]); printf(" to return the host\n"); printf(" %s 127.0.0.1\n", argv[0]); printf(" to return the IP addresses for a host\n"); printf(" %s www.contoso.com\n", argv[0]); return 1; } // Initialize Winsock iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { printf("WSAStartup failed: %d\n", iResult); return 1; } host_name = argv[1]; // If the user input is an alpha name for the host, use gethostbyname() // If not, get host by addr (assume IPv4) if (isalpha(host_name[0])) { /* host address is a name */ printf("Calling gethostbyname with %s\n", host_name); remoteHost = gethostbyname(host_name); } else { printf("Calling gethostbyaddr with %s\n", host_name); addr.s_addr = inet_addr(host_name); if (addr.s_addr == INADDR_NONE) { printf("The IPv4 address entered must be a legal address\n"); return 1; } else remoteHost = gethostbyaddr((char *) &addr, 4, AF_INET); } if (remoteHost == NULL) { dwError = WSAGetLastError(); if (dwError != 0) { if (dwError == WSAHOST_NOT_FOUND) { printf("Host not found\n"); return 1; } else if (dwError == WSANO_DATA) { printf("No data record found\n"); return 1; } else { printf("Function failed with error: %ld\n", dwError); return 1; } } } else { printf("Function returned:\n"); printf("\tOfficial name: %s\n", remoteHost->h_name); for (pAlias = remoteHost->h_aliases; *pAlias != 0; pAlias++) { printf("\tAlternate name #%d: %s\n", ++i, *pAlias); } printf("\tAddress type: "); switch (remoteHost->h_addrtype) { case AF_INET: printf("AF_INET\n"); break; case AF_INET6: printf("AF_INET6\n"); break; case AF_NETBIOS: printf("AF_NETBIOS\n"); break; default: printf(" %d\n", remoteHost->h_addrtype); break; } printf("\tAddress length: %d\n", remoteHost->h_length); if (remoteHost->h_addrtype == AF_INET) { while (remoteHost->h_addr_list[i] != 0) { addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++]; printf("\tIPv4 Address #%d: %s\n", i, inet_ntoa(addr)); } } else if (remoteHost->h_addrtype == AF_INET6) printf("\tRemotehost is an IPv6 address\n"); } return 0; }

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧