读书人

windows上vs2010显示本机ip无法通过

发布时间: 2013-01-07 10:02:25 作者: rapoo

windows下vs2010显示本机ip无法通过,源码如下,请求指教,谢谢
#include "StdAfx.h"
#include<afx.h>
#include<winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main(int argc,char**argv)
{

struct hostent*hn;
CString strIPAddr;
char HostName[32];
if(gethostname(HostName,sizeof(HostName))==0)
{
if( hn=gethostbyname(HostName))
{
strIPAddr=inet_ntoa(*(struct in_addr*)hn->h_addr_list[0]);
cout<<strIPAddr<<endl
}

}
return 0;
}
[解决办法]

引用:
可能需要先WSAStartup

++
struct hostent*hn=new hostent;

读书人网 >C++

热点推荐