除了getdomainname()之外,有没有其他方法可以用C在Mac上获取域名?
我也可以使用Cocoa API。谢谢。
发布于 2011-12-02 00:41:18
如果您正在查找与本地系统对应的DNS名称,我不认为getdomainname()是您的朋友。据the documentation介绍,这是为了获取系统的NIS域名:
Getdomainname() returns the standard NIS domain name for the current
host, as previously set by setdomainname(). The parameter namelen speci-fies specifies
fies the size of the name array. The returned name is null-terminated
unless insufficient space is provided.事实上,说主机没有DNS域名在很大程度上是准确的。系统上的每个接口都有一个地址,可能有解析到该地址的DNS记录,但事实是(a)可能没有对应的DNS记录,(b)可能有解析到您的主机和其他系统的DNS记录。例如,主机名"google.com“解析为一系列不同的地址:
$ host -t A google.com
google.com has address 74.125.226.244
google.com has address 74.125.226.240
google.com has address 74.125.226.241
google.com has address 74.125.226.242
google.com has address 74.125.226.243当然,您的系统可能有多个与之关联的IP地址(例如,有线和无线)。
如果您愿意祈祷,您最好的选择通常是通过调用打开的连接上的getsockname()来确定您的本地ip地址,然后在该地址上进行反向DNS查找,以在DNS中找到相应的名称。
https://stackoverflow.com/questions/8343895
复制相似问题