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

c语言获取主机ip

发布时间: 2022-02-14 07:43:11

1. 用c语言编程显示主机IP地址!

最简单的方法是用 ipconfig
为了把数取出来,放到程序里好使用,可以把 ipconfig内容 存入C:\\TEMP\\tmp.ip, 再取出来用。
下面程序运行完,IP 的 4 段 整数 存在 d1,d2,d3,d4 里。

#include <stdio.h>
void main()
{
FILE *fin;
char namein[]="C:\\TEMP\\tmp.ip";
char str1[64],str2[64];
char one_line[80];
char cmd[120];
int i,j,k;
int d1,d2,d3,d4;

sprintf(cmd,"ipconfig > %s",namein);
system(cmd);
fin = fopen(namein,"r");
while ( fgets(&one_line[0],80,fin) !=NULL ){
j = sscanf(one_line,"%s %s",&str1[0],&str2[0]);
if (j == 2 && strncmp(str2,"Address",7) ==0 ) {
for (i=1;i<50;i++) if (one_line[i] ==':') k = i;
break;
};
};
fclose(fin);
sscanf(&one_line[0]+k+1,"%d.%d.%d.%d", &d1,&d2,&d3,&d4);
printf("%d %d %d %d\n",d1,d2,d3,d4);
exit(0);
}

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

3. 请问 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));

4. 如何用C语言编程得到本地主机得IP和主机名

  1. c语言本身是不提供的。

  2. 在windows下使用系统命令或windows.h

  3. //列子

  4. #include"stdio.h"
    #include"conio.h"
    main()
    {
    inti,j;
    charip[20];
    chartemp[100];
    charch='';
    FILE*fp;
    system("ipconfig>d:\myip.txt");
    if((fp=fopen("d:\myip.txt","r"))==NULL)
    {
    printf("thefilecannotopen: Pressanykeytoexit:");
    getch();
    exit(1);
    }
    for(i=0;i<7;i++)
    {fgets(temp,80,fp);/*跳过一些行*/
    /*printf("%s ",temp);*/}
    fgets(temp,80,fp);
    i=0;j=0;
    while(temp[i++]!=':')
    ;
    while(temp[i]!=' ')
    ip[j++]=temp[i++];
    ip[j]=0;
    printf("IP=%s ",ip);
    fclose(fp);
    system("deld:\myip.txt");

    getch();
    }
  5. 在Linux下
  6. #include<stdio.h>;
    #include<sys/types.h>;
    #include<sys/socket.h>;
    #include<sys/ioctl.h>;
    #include<netinet/in.h>;
    #include<net/if.h>;
    #include<net/if_arp.h>;
    #include<arpa/inet.h>;
    #include<errno.h>;

    #defineETH_NAME"eth0"

    intmain()
    {
    intsock;
    structsockaddr_insin;
    structifreqifr;

    sock=socket(AF_INET,SOCK_DGRAM,0);
    if(sock==-1)
    {
    perror("socket");
    return-1;
    }

    strncpy(ifr.ifr_name,ETH_NAME,IFNAMSIZ);
    ifr.ifr_name[IFNAMSIZ-1]=0;

    if(ioctl(sock,SIOCGIFADDR,&ifr)<0)
    {
    perror("ioctl");
    return-1;
    }

    memcpy(&sin,&ifr.ifr_addr,sizeof(sin));
    fprintf(stdout,"eth0:%s ",inet_ntoa(sin.sin_addr));

    return0;
    }

5. c语言调用本机ip地址时如何定义gethostname函数

函数声明为

int PASCAL FAR gethostname(char FAR *name, int namelen);

所以 , 应该是

charname[65];
intret=gethostname(name,sizeof(name));
if(ret==0)printf("nameis%s ",name);
elseprintf("getfailedret=%d ",ret);

6. Dev_c++中C语言如何获得台式本机的IP

#include <winsock2.h>
#include <iostream.h>

int main()
{
char hostname[100] ;
struct hostent *phostinfo ;
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
cout << " winsock start up error !" ;
return;
}
gethostname(hostname,strlen(hostname));
phostinfo = gethostbyname(hostname);

cout << inet_ntoa( *(in_addr *) phostinfo->h_addr ) ;

}

7. 如何用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));

8. 请问 , 用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( );
}

9. 如何只用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;
}

10. C语言查询本机ip地址的程序

#include <windows.h>
#include <winsock2.h>