c获取访问ip
① 请问 C# 如何获取外网IP
本机是获取不到自己外网IP的,真的想要知道,你可以访问ip138这种网站,让这种网站获得你的IP再返回给你。有点儿像自己的眼睛是看不到自己长啥样,想要看得是镜子“获得”你的样子,然后再返还给你一样。
路由什么的是基于NAT的,根本不需要知道客户端的真实地址是什么,服务端地址固定住就好了。当客户端与服务端连接时(建立Sockets)经过路由,路由会NAT给客户端一个地址(包含IP和端口号两部分),服务端只需要往这个地址上发送信息,路由器识别你这个地址会自动转发给相应的真正的客户端的,这也就是路由的本身作用。
② 请问 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地址啊 谢谢了
看你要获得那里的ip地址
如果是本及机的话,就使用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( );
}
④ C语言能获取当前IP吗
#include "stdio.h"
#include "conio.h"
main()
{
int i,j;
char ip[20];
char temp[100];
char ch='\0';
FILE *fp;
system("ipconfig >d:\\myip.txt");
if ((fp=fopen("d:\\myip.txt","r"))==NULL)
{
printf("the file can not open:\nPress any key to exit:");
getch();
exit(1);
}
for (i=0;i<7;i++)
{fgets(temp,80,fp); /*跳过一些行*/
/*printf("%s\n",temp); */}
fgets(temp,80,fp);
i=0;j=0;
while (temp[i++]!=':')
;
while (temp[i]!='\n')
ip[j++]=temp[i++];
ip[j]=0;
printf("IP=%s\n",ip);
fclose(fp);
system("del d:\\myip.txt");
getch();
}
⑤ 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();
}
⑥ c语言获取客户端IP
SOCKADDR换成SOCKADDR_IN
⑦ 如何获取网络设备IP地址
需要作个ip地址规划,比如多少机器,怎么访问等,ip地址是公有还是私有,是否只是内部用等。举例,内部1个c段私有地址192.168.200.0/24,有15台机器,5个一个网段,另外10个一个网段,就可以将这个c分成8个或者16个子网,每个子网32或者16个地址。用192.168.200.0/255.255.255.248和192.168.200.32/255.255.255.248这两个子网可满足现状,也有一定的可扩展性。
⑧ 如何用C语言获得本机IP地址
structin_addraddr;
hostent*pHost=::gethostbyname("localhost");//在此写入你自己电脑主机名字
switch(pHost->h_addrtype){
caseAF_INET:
printf("internet网络地址类型(AF_INET) ");
break;
caseAF_INET6:
printf("internet网络地址类型(AF_INET) ");
break;
caseAF_NETBIOS:
printf("netbios网络地址类型(AF_NETBIOS) ");
break;
default:
printf("其它地址类型%d ",pHost->h_addrtype);
break;
}
printf(" 地址长度:%d(字节) ",pHost->h_length);
addr.s_addr=*(u_long*)pHost->h_addr_list[0];
printf(" 第一个IP地址为:%s ",inet_ntoa(addr));
⑨ 如何获取局域网内所有登陆电脑的ip ,求C语言代码!
C++ 获取局域网内所有可用IP和主机名;
参考如下:
void GetNameAndIp()
{
struct hostent *host;
struct in_addr *ptr;
DWORD dwScope = RESOURCE_CONTEXT;
NETRESOURCE *NetResource = NULL;
HANDLE hEnum;
WNetOpenEnum(dwScope, NULL, NULL, NULL, &hEnum);
WSADATA wsaData;
WSAStartup(MAKEWORD(1, 1), &wsaData);
if (hEnum)
{
DWORD Count = 0xFFFFFFFF;
DWORD BufferSize = 10240;
LPVOID Buffer = new char[10240];
WNetEnumResource(hEnum, &Count, Buffer, &BufferSize);
NetResource = (NETRESOURCE*)Buffer;
char szHostName[200];
for (unsigned int i = 0; i < BufferSize / sizeof(NETRESOURCE); i++, NetResource++)
{
if (NetResource->dwUsage == RESOURCEUSAGE_CONTAINER && NetResource->dwType == RESOURCETYPE_ANY)
{
if (NetResource->lpRemoteName)
{
CString strFullName = 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 = (struct in_addr *) host->h_addr_list[0];
string str = "";
for (int n = 0; n<4; n++)
{
CString addr;
if (n > 0)
{
str += ".";
}
int value = (unsigned int)((unsigned char*)host->h_addr_list[0])[n];
char p[20];
sprintf(p, "%d", value);
str.append(p);
}
std::cout <<"IP:"<< str <<" Name:"<<host->h_name<< std::endl;
}
}
}
delete Buffer;
WNetCloseEnum(hEnum);
}
WSACleanup();
}