如果是本及机的话,就使用windows的API啊
#include "winsock.h"
WORD wVersionRequested
WSADATA wsaData
char name[255]
char* ip
PHOSTENT hostinfo
wVersionRequested = MAKEWORD( 2, 0 )
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list)
}
}
// ip is ready
WSACleanup( )
}
#include<windows.h>#include<string>
#include<cstdio>
using namespace std
int main()
{
system("ipconfig /all")//使用运行CMD.exe,并输入ipconfig /all,可得出本机所有物理地址信息(IP信息)
getchar()//获取记录信息
return 0
}
struct in_addr addrhostent *pHost = ::gethostbyname("localhost")//在此写入你自己电脑主机名字
switch (pHost->h_addrtype) {
case AF_INET:
printf("internet网络地址类型(AF_INET)\n")
break
case AF_INET6:
printf("internet网络地址类型(AF_INET)\n")
break
case AF_NETBIOS:
printf("netbios网络地址类型(AF_NETBIOS)\n")
break
default:
printf("其它地址类型 %d\n", pHost->h_addrtype)
break
}
printf("\t地址长度: %d(字节)\n", pHost->h_length)
addr.s_addr = *(u_long *) pHost->h_addr_list[0]
printf("\t第一个IP地址为: %s\n", inet_ntoa(addr))
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)