如何获得域名的IP

ndb96 2007-03-16 10:36:17
从来没用过C,现在要改一个linux下的C程序,把原来的静态IP换成由域名来解析IP,程序里有一句
char *pHost="218.46.190.25";

这样改行不行?
char *pHost=gethostbyname("www.abc.com");

感觉不太对劲,程序后面还要引用这个pHost
请问怎么改?
...全文
433 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ndb96 2007-03-17
  • 打赏
  • 举报
回复
我倒,我就是用java的,不过还是感谢楼上的关注
ndb96 2007-03-17
  • 打赏
  • 举报
回复
不会这么复杂吧,别的语言都是一个转换命令就搞定了
ndb96 2007-03-17
  • 打赏
  • 举报
回复
真的没有简单的方法来获得域名的IP吗?
jixingzhong 2007-03-17
  • 打赏
  • 举报
回复
dns.c
http://www.opensource.apple.com/darwinsource/Current/OpenSSH-57.3/openssh/dns.c

自己看看?
jixingzhong 2007-03-17
  • 打赏
  • 举报
回复
Linux下套接口技术编写网络域名解析程序
http://scholar.ilib.cn/abstract.aspx?A=xddzjs200107009
laiwusheng 2007-03-16
  • 打赏
  • 举报
回复
看错了,是linux下的?
laiwusheng 2007-03-16
  • 打赏
  • 举报
回复
//Java版的
import java.net.InetAddress;
import java.net.UnknownHostException;
ublic class NsLookup {
tatic public void main(String[] args) {
try {
InetAddress address =
InetAddress.getByName(args[0]);
System.out.println(args[0]+" : "+address.getHostAddress());
}
catch(UnknownHostException uhe) {
System.err.println ("Unable to find: "+args[0]);
}
}
}
//VC版
CString sIPAddress;
struct hostent FAR *lpHostEnt=gethostbyname("www.yahoo.com");
if(lpHostEnt == NULL)
{
sIPAddress = _T("");
}
LPSTR lpaddr = lpHostEnt->h_addr_list[0];
if(lpaddr)
{
struct in_addr inAddr;
memmove (&inAddr,lpaddr, 4);
sIPAddress=inet_ntoa (inAddr);
if(sIPAddress.IsEmpty())
sIPAddress=_T("Not available");
}
AfxMessageBox(sIPAddress);

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧