C源码:经过IP地址查找主机名 findip.c-C / C++-优质IT资源分享社区

admin
管理员
管理员
  • UID1
  • 粉丝26
  • 关注4
  • 发帖数581
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:167回复:0

  C源码:经过IP地址查找主机名 findip.c

楼主#
更多 发布于:2016-05-22 14:59

/*

* This program is free software; you can

redistribute it and/or modify

* it under the terms of the GNU General Public

License as published by

* the Free Software Foundation; either version 2

of the License, or

* (at your option) any later version.

*

* This program is distributed in the hope that it

will be useful,

* but WITHOUT ANY WARRANTY; without even the

implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR

PURPOSE. See the

* GNU General Public License for more details.

*

* You should have received a copy of the GNU

General Public License

* along with this program; if not, write to the

Free Software

* Foundation, Inc., 675 Mass Ave, Cambridge, MA

02139, USA.

*

*

* Filename: findip.c

* Created : Nov. 10th 1995 @ 4:30pm

* Author : SrfRoG (srfrog@itek.net)

* Descrip.: This cute little program will attempt

to resolve

* an IP address to its hostname. Then prints the

results

* to standard output.

*

---------------------------------------------------------------------

* To compile this program:

* Linux: gcc -O2 -m486 -s -o findip findip.c

* AIX, BSD: gcc -O2 -s -o findip findip.c

* SunOS: gcc -O2 -lnsl -s -o findip findip.c

* Other: cc -o findip findip.c

*/

#include

#include

#include

#include

#include

#include

int main(argc, argv)

int argc;

char **argv;

{

struct hostent *ip;

unsigned long hostname;

if (argc != 2)

{

printf("Need to specify an IP address.\n");

exit(1);

}

if ((hostname = inet_addr(argv)) == -1)

{

printf("Could not find %s\n", argv);

exit(1);

}

if ((ip = gethostbyaddr((char *)&hostname,

sizeof(long), AF_INET)) != NULL)

printf("%s is %s\n", argv, ip->h_name);

else

printf("Could not resolve %s\n", argv);

return 0;

}

优质IT资源分享社区为你提供此文。

本站有大量优质C、C++教程视频,资料等资源,包含C,C++基础教程,高级进阶教程等等,教程视频资源涵盖传智播客,极客学院,达内,北大青鸟,猎豹网校等等IT职业培训机构的培训教学视频,价值巨大。欢迎点击下方链接查看。

C、C++教程视频

优质IT资源分享社区(www.itziyuan.top)
一个免费,自由,开放,共享,平等,互助的优质IT资源分享网站。
专注免费分享各大IT培训机构最新培训教学视频,为你的IT学习助力!

!!!回帖受限制请看点击这里!!!
!!!资源失效请在此版块发帖说明!!!

[PS:按 CTRL+D收藏本站网址~]

——“优质IT资源分享社区”管理员专用签名~

本版相似帖子

游客