c语言ip地址
发布时间: 2024-03-04 04:24:56
① 请问 c语言怎样获取计算机ip地址啊
struct in_addr addr;
hostent *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));
② C语言IP地址查询系统如何实现
C语言如何实现IP地址查询系统
参考如下:
voidGetNameAndIp()
{
structhostent*host;
structin_addr*ptr;
DWORDdwScope=RESOURCE_CONTEXT;
NETRESOURCE*NetResource=NULL;
HANDLEhEnum;
WNetOpenEnum(dwScope,NULL,NULL,NULL,&hEnum);
WSADATAwsaData;
WSAStartup(MAKEWORD(1,1),&wsaData);
if(hEnum)
{
DWORDCount=0xFFFFFFFF;
DWORDBufferSize=10240;
LPVOIDBuffer=newchar[10240];
WNetEnumResource(hEnum,&Count,Buffer,&BufferSize);
NetResource=(NETRESOURCE*)Buffer;
charszHostName[200];
for(unsignedinti=0;i<BufferSize/sizeof(NETRESOURCE);i++,NetResource++)
{
if(NetResource->dwUsage==RESOURCEUSAGE_CONTAINER&&NetResource->dwType==RESOURCETYPE_ANY)
{
if(NetResource->lpRemoteName)
{
CStringstrFullName=NetResource->lpRemoteName;
if(0==strFullName.Left(2).Compare(_T("\\")))
strFullName=strFullName.Right(strFullName.GetLength()-2);
gethostname(szHostName,strlen(szHostName));
USES_CONVERSION;
char*pchar=T2A(strFullName);
host=gethostbyname(pchar);
if(host==NULL)continue;
ptr=(structin_addr*)host->h_addr_list[0];
stringstr="";
for(intn=0;n<4;n++)
{
CStringaddr;
if(n>0)
{
str+=".";
}
intvalue=(unsignedint)((unsignedchar*)host->h_addr_list[0])[n];
charp[20];
sprintf(p,"%d",value);
str.append(p);
}
std::cout<<"IP:"<<str<<"Name:"<<host->h_name<<std::endl;
}
}
}
deleteBuffer;
WNetCloseEnum(hEnum);
}
WSACleanup();
}
热点内容