当前位置: 技术问答>linux和unix
怎么在linux下获得本机ip地址?(我是新手,别见笑
来源: 互联网 发布时间:2014-11-03
本文导语: ?? | #include #include #include #include #include int main(int argc,char *argv[]) { char * host,**names,**addrs; struct hostent *hostinfo; if(argc == 1) { char myname[256]; ...
??
|
#include
#include
#include
#include
#include
int
main(int argc,char *argv[])
{
char * host,**names,**addrs;
struct hostent *hostinfo;
if(argc == 1)
{
char myname[256];
gethostname(myname,255);
host=myname;
} else if(argc != 2)
{
printf("Usage:%s n",argv[0]);
exit(0);
}else
host=argv[1];
hostinfo=gethostbyname(host);
if(!hostinfo)
{
printf("can't get info of host: %sn",host);
exit(1);
}
printf("-------%s:infomation-----n",host);
printf("Offical name:%sn",hostinfo->h_name);
printf("Alias:n");
names=hostinfo->h_aliases;
while(*names){
printf("%sn",*names);
names++;
}
if(hostinfo->h_addrtype != AF_INET)
{
printf("not a IP hostn");
exit(1);
}
printf("Address:n");
addrs=hostinfo->h_addr_list;
while(*addrs)
{
printf("%sn",inet_ntoa(*(struct in_addr *)*addrs));
addrs++;
}
printf("-------info end-----n----http://www.calihotel.net----");
exit(0);
}
#include
#include
#include
#include
int
main(int argc,char *argv[])
{
char * host,**names,**addrs;
struct hostent *hostinfo;
if(argc == 1)
{
char myname[256];
gethostname(myname,255);
host=myname;
} else if(argc != 2)
{
printf("Usage:%s n",argv[0]);
exit(0);
}else
host=argv[1];
hostinfo=gethostbyname(host);
if(!hostinfo)
{
printf("can't get info of host: %sn",host);
exit(1);
}
printf("-------%s:infomation-----n",host);
printf("Offical name:%sn",hostinfo->h_name);
printf("Alias:n");
names=hostinfo->h_aliases;
while(*names){
printf("%sn",*names);
names++;
}
if(hostinfo->h_addrtype != AF_INET)
{
printf("not a IP hostn");
exit(1);
}
printf("Address:n");
addrs=hostinfo->h_addr_list;
while(*addrs)
{
printf("%sn",inet_ntoa(*(struct in_addr *)*addrs));
addrs++;
}
printf("-------info end-----n----http://www.calihotel.net----");
exit(0);
}