当前位置:首页 » 编程语言 » c语言获取别人ip代码
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言获取别人ip代码

发布时间: 2022-12-15 04:43:41

❶ 请问 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地址

#include<stdio.h>//printf
#include<string.h>//memset
#include<stdlib.h>//forexit(0);
#include<sys/socket.h>
#include<errno.h>//Forerrno-theerrornumber
#include<netdb.h>//hostent
#include<arpa/inet.h>

inthostname_to_ip(char*,char*);

intmain(intargc,char*argv[])
{
if(argc<2)
{
printf("");
exit(1);
}

char*hostname=argv[1];
charip[100];

hostname_to_ip(hostname,ip);
printf("%sresolvedto%s",hostname,ip);

printf(" ");

}
/*
Getipfromdomainname
*/

inthostname_to_ip(char*hostname,char*ip)
{
structhostent*he;
structin_addr**addr_list;
inti;

if((he=gethostbyname(hostname))==NULL)
{
//getthehostinfo
herror("gethostbyname");
return1;
}

addr_list=(structin_addr**)he->h_addr_list;

for(i=0;addr_list[i]!=NULL;i++)
{
//Returnthefirstone;
strcpy(ip,inet_ntoa(*addr_list[i]));
return0;
}

return1;
}

❸ 请问 , 用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语言获取Linux系统的网卡IP地址

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<net/if.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<sys/ioctl.h>

//获取地址
//返回IP地址字符串
intgetlocalip(char*outip)
{
inti=0;
intsockfd;
structifconfifconf;
charbuf=(char)malloc(512);
structifreq*ifreq;
char*ip;

//初始化ifconf
ifconf.ifc_len=512;
ifconf.ifc_buf=buf;

if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
return-1;
}
ioctl(sockfd,SIOCGIFCONF,&ifconf);//获取所有接口信息
close(sockfd);
//接下来一个一个的获取IP地址
ifreq=(structifreq*)buf;
i=ifconf.ifc_len/sizeof(structifreq);
char*pos=outip;
intcount;
for(count=0;(count<5&&i>0);i--)
{
ip=inet_ntoa(((structsockaddr_in*)&(ifreq->ifr_addr))->sin_addr);

if(strncmp(ip,"127.0.0.1",3)==0)//排除127.x.x.x,继续下一个
{
ifreq++;
continue;
}else
{
printf("%s ",ip);
strcpy(pos,ip);
intlen=strlen(ip);
pos=' ';
pos+=len+1;
count++;
ifreq++;
}
}
free(buf);
return0;
}
//——————————-函数的调用方式————————————-
intmain(intargc,char**argv)
{
charip={'*'};

if(getlocalip(ip)==0)
{
printf("本机IP地址是:%s ",ip);
}
else
{
printf("无法获取本机IP地址");
}
return0;
}

❺ c语言编程 实现ip地址查找 方法用二进制trie

不知道您有没有学习过计算机网络。如果没有,下面简单介绍一下IP地址和掩码。
IP地址是32位的,也就是4字节。取值可以任意。
掩码也是32位的,一般建议前面的位都为1,后面的位都为0。所以本题:
(1)只要是32位的都是有效的。
(2)判断是否全1后面接全0.
(3)如果掩码的前n位为1,那么此IP所在网络全部有效IP地址是此IP地址的前n位不变,后面的位从全0到全1,比如IP地址为1.1.1.1掩码为FFFF0000,那么此IP地址所在网络的全部有效IP地址是从1.1.0.0到1.1.255.255。
(4)两个IP地址都与掩码进行二进制与,看得到的结果是否相同,如果相同则在同一子网中,否则不在同一子网中。这个用c很容易实现。

❻ 只用C语言,怎么通过域名得到IP地址

查找的方法(07版为例): 1、在左下方开始键,点击之后出现搜索框 2、在搜索框内输入指令cmd运行 3、进入窗口,按照要求输入指令:nbtstat -a IP,ip地址为你所查询主机的ip地址。 4、这样就可以根据ip泛解析出域名。

❼ 用C语言编写程序如何获得外网的IP的地址

//通过ip138网站来查询外网IP的
#include <afxinet.h>
void CLanChatDlg::GetNetIP()
{

SetDlgItemText(IDC_NET_IP,"正在获取外网IP");
CString strsource;
CString Address;
CInternetSession mySession(NULL,0);
CHttpFile* myHttpFile=NULL;

Address="http://www.ip138.com/ip2city.asp";//ip138网页

myHttpFile=(CHttpFile*)mySession.OpenURL(Address);//读取网络地址

while(myHttpFile->ReadString(strsource))
{ //循环读取下载来的网页文本
// AddToLog(strsource);
int begin=0;
begin=strsource.Find("[",0);

if(begin!=-1)//如果找到"[", 则找"]" 中括号内的文本则是 你的外网ip
{ int end=strsource.Find("]");
m_internetip=strsource.Mid(begin+1,end-begin-1);//提取外网ip

SetDlgItemText(IDC_NET_IP,m_internetip);//在左下角显示外网ip
}
}

❽ 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的程序

输入 域名
结果的 4小节整数 存 int d1,d2,d3,d4 变量中。

#include <stdio.h>
#include <stdlib.h>

void main()
{
char ym[80]="www..com";
FILE *fp;
char cmd[120];
char nameout[]="tmp_ping.txt"; // 临时文件
char str3[32];
int i,len,d1,d2,d3,d4;

printf("Enter domain address\n");
scanf("%s",&ym[0]);
sprintf(cmd,"ping %s -n 1 > %s\0",ym,nameout);
system(cmd);
fp = fopen(nameout,"r");
fscanf(fp,"%*s %*s %s",&str3[0]);
fclose(fp);
len = strlen(str3);
for (i=0;i<len;i++) if ( str3[i] == '[' || str3[i] == ']'|| str3[i] == '.') str3[i] = ' ';
sscanf(str3, "%d %d %d %d",&d1,&d2,&d3,&d4);
printf("ip=%d.%d.%d.%d\n",d1,d2,d3,d4);

sprintf(cmd,"ERASE /F /Q %s\0",nameout); // 删去临时文件
system(cmd);
getchar();
}

程序未考虑错误域名和无IP返回的域名,你可以自己补充几行处理程序。

❿ c语言获取客户端IP

SOCKADDR换成SOCKADDR_IN