如何获取本机IP地址

Dev-C++ version

 

#include  < iostream >
#include 
< winsock2.h >

using   namespace  std;

bool  GetLocalIP( char   *  ipBuff);

int  main()
{
    WORD wVersionRequested 
= MAKEWORD(2,2);
    WSADATA data;
    
    
int err;
    err 
= WSAStartup(wVersionRequested, &data);
    
if ( err != 0 ) {
        
/* Tell the user that we could not find a usable */
        
/* WinSock DLL.                                  */
        cout 
<< "We could not find a usable winsock DLL!" << endl;
        
return 0;
    }


    
char IP[16= 0 };
    
if( GetLocalIP( IP ) )
        cout 
<< IP << endl;
    
else
        cout 
<< "Failed to GetLocalIP() !" << endl;
    
    WSACleanup();
    
    system(
"PAUSE");
    
    
return 0;
}



bool  GetLocalIP( char   *  ipBuff)
{
    
if ( ipBuff == NULL )
        
return false;
    
char hostName[80= 0 };
    
if ( gethostname( hostName , 80 ) == SOCKET_ERROR )
        
return false;
    
struct hostent * phe = gethostbyname( hostName );
    
if ( phe == NULL )
        
return false;
    
if ( phe->h_addr_list[0== 0 )
        
return false;
    
struct in_addr addr;
    memcpy( 
&addr, phe->h_addr_list[0] , sizeofstruct in_addr ) );

    
char * tempIP = inet_ntoa( addr );
    memcpy(ipBuff, tempIP , strlen(tempIP) );

    
return true;
}

 

VC++ version

 

#include  < iostream.h >
#include 
< winsock2.h >

bool  GetLocalIP( char   *  ipBuff);

void  main()
{
    WORD wVersionRequested 
= MAKEWORD(2,2);
    WSADATA data;
    
    
int err;
    err 
= WSAStartup(wVersionRequested, &data);
    
if ( err != 0 ) {
        
/* Tell the user that we could not find a usable */
        
/* WinSock DLL.                                  */
        cout 
<< "We could not find a usable winsock DLL!" << endl;
        
return;
    }


    
char IP[16= 0 };
    
if( GetLocalIP( IP ) )
        cout 
<< IP << endl;
    
else
        cout 
<< "Failed to GetLocalIP() !" << endl;
    
    WSACleanup();

    system(
"PAUSE");
}



bool  GetLocalIP( char   *  ipBuff)
{
    
if ( ipBuff == NULL )
        
return false;
    
char hostName[80= 0 };
    
if ( gethostname( hostName , 80 ) == SOCKET_ERROR )
        
return false;
    
struct hostent * phe = gethostbyname( hostName );
    
if ( phe == NULL )
        
return false;
    
if ( phe->h_addr_list[0== 0 )
        
return false;
    
struct in_addr addr;
    memcpy( 
&addr, phe->h_addr_list[0] , sizeofstruct in_addr ) );

    
char * tempIP = inet_ntoa( addr );
    memcpy(ipBuff, tempIP , strlen(tempIP) );

    
return true;
}

下面是GetLocalIP的重载函数,在设置控件的时候可以用到:

 

bool  GetLocalIP(BYTE &  nField0 , BYTE &  nField1 , BYTE &  nField2 , BYTE &  nField3)
{
    nField0 
= nField1 = nField2 = nField3 = 0;
    
char hostName[80= 0 };
    
if ( gethostname( hostName , 80 ) == SOCKET_ERROR )
        
return false;
    
struct hostent * phe = gethostbyname( hostName );
    
if ( phe == NULL )
        
return false;
    
if ( phe->h_addr_list[0== 0 )
        
return false;
    
struct in_addr addr;
    memcpy( 
&addr, phe->h_addr_list[0] , sizeofstruct in_addr ) );

    
char * tempIP = inet_ntoa( addr );
    UINT size 
= strlen( tempIP );
    UINT pos 
= 0;
    
while ( tempIP[pos] != '.' ){
        nField0 
= nField0*10 + (tempIP[pos++- 0x30);
    }

    pos
++;
    
while ( tempIP[pos] != '.' ){
        nField1 
= nField1*10 + (tempIP[pos++- 0x30);
    }

    pos
++;
    
while ( tempIP[pos] != '.' ){
        nField2 
= nField2*10 + (tempIP[pos++- 0x30);
    }

    pos
++;
    
while ( tempIP[pos] != '/0' ){
        nField3 
= nField3*10 + (tempIP[pos++- 0x30);
    }

    
return true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值