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();
}