Objective C中如何从域名中获取IP地址和IP地址的域名?

发布于 2024-09-15 21:50:04 字数 300 浏览 2 评论 0原文

我可以通过使用此问题的答案获取我正在使用的设备/机器的当前 IP 地址。

我已经解决了这个问题。 Java允许从域名获取IP地址。在 Objective C 中可能吗?如何?

第二个问题是如何使用 IP 地址获取设备/机器的名称。举例来说,我有一个 IP 地址 192.168.0.74 = 设备名称是什么?在目标 C 中?

I am able to get the current IP address of my device/machine that I am using - by using this question's answer.

I have gone through this question. Java allows to get the IP address from a domain name. Is it possible in Objective C? How?

The second question is How to get the name of device/machine by using its IP address. Say for example I have an IP address 192.168.0.74 = What is the device name? in Objective C?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执手闯天涯 2024-09-22 21:50:04

我不确定这是否是最好的方法,但它大多数情况下对我有用。我输入 StackOverflow 的 IP 地址 (69.59.196.211),它返回了 stackoverflow.com,但我输入了 Google 的 IP 地址之一 (210.55.180.158),它返回了 缓存.googlevideo.com(适用于所有结果,而不仅仅是第一个结果)。

int error;
struct addrinfo *results = NULL;

error = getaddrinfo("69.59.196.211", NULL, NULL, &results);
if (error != 0)
{
    NSLog (@"Could not get any info for the address");
    return; // or exit(1);
}

for (struct addrinfo *r = results; r; r = r->ai_next)
{
    char hostname[NI_MAXHOST] = {0};
    error = getnameinfo(r->ai_addr, r->ai_addrlen, hostname, sizeof hostname, NULL, 0 , 0);
    if (error != 0)
    {
        continue; // try next one
    }
    else
    {
        NSLog (@"Found hostname: %s", hostname);
        break;
    }
}

freeaddrinfo(results);

该地址可以有多个名称,因此您可能不想在找到的第一个名称处停止。

I'm not sure if this is the best way to do this, but it works for me, mostly. I put in StackOverflow's IP addresses (69.59.196.211) and it gave me back stackoverflow.com, but I put in one of Google's IP addresses (210.55.180.158) and it gave me back cache.googlevideo.com (for all results, not just the first one).

int error;
struct addrinfo *results = NULL;

error = getaddrinfo("69.59.196.211", NULL, NULL, &results);
if (error != 0)
{
    NSLog (@"Could not get any info for the address");
    return; // or exit(1);
}

for (struct addrinfo *r = results; r; r = r->ai_next)
{
    char hostname[NI_MAXHOST] = {0};
    error = getnameinfo(r->ai_addr, r->ai_addrlen, hostname, sizeof hostname, NULL, 0 , 0);
    if (error != 0)
    {
        continue; // try next one
    }
    else
    {
        NSLog (@"Found hostname: %s", hostname);
        break;
    }
}

freeaddrinfo(results);

There can be multiple names for the address, so you might not want to stop at the first one you find.

暮年 2024-09-22 21:50:04

我写了一个已接受答案的 Swift 版本,尽管我不能 100% 确定它的正确性。

func reverseDNS(ip: String) -> String {
    var results: UnsafeMutablePointer<addrinfo>? = nil
    defer {
        if let results = results {
            freeaddrinfo(results)
        }
    }
    let error = getaddrinfo(ip, nil, nil, &results)
    if (error != 0) {
        print("Unable to reverse ip: \(ip)")
        return ip
    }

    for addrinfo in sequence(first: results, next: { $0?.pointee.ai_next }) {
        guard let pointee = addrinfo?.pointee else {
            print("Unable to reverse ip: \(ip)")
            return ip
        }

        let hname = UnsafeMutablePointer<Int8>.allocate(capacity: Int(NI_MAXHOST))
        defer {
            hname.deallocate()
        }
        let error = getnameinfo(pointee.ai_addr, pointee.ai_addrlen, hname, socklen_t(NI_MAXHOST), nil, 0, 0)
        if (error != 0) {
            continue
        }
        return String(cString: hname)
    }

    return ip
}

I wrote a Swift version of the accepted answer, though I'm not 100% sure of its correctness.

func reverseDNS(ip: String) -> String {
    var results: UnsafeMutablePointer<addrinfo>? = nil
    defer {
        if let results = results {
            freeaddrinfo(results)
        }
    }
    let error = getaddrinfo(ip, nil, nil, &results)
    if (error != 0) {
        print("Unable to reverse ip: \(ip)")
        return ip
    }

    for addrinfo in sequence(first: results, next: { $0?.pointee.ai_next }) {
        guard let pointee = addrinfo?.pointee else {
            print("Unable to reverse ip: \(ip)")
            return ip
        }

        let hname = UnsafeMutablePointer<Int8>.allocate(capacity: Int(NI_MAXHOST))
        defer {
            hname.deallocate()
        }
        let error = getnameinfo(pointee.ai_addr, pointee.ai_addrlen, hname, socklen_t(NI_MAXHOST), nil, 0, 0)
        if (error != 0) {
            continue
        }
        return String(cString: hname)
    }

    return ip
}
猫烠⑼条掵仅有一顆心 2024-09-22 21:50:04

您需要读取路由表 - 基本上与“netstat -r”命令相同的方式。 netstat 实现是开源的 - 它位于

http

: //www.opensource.apple.com/release/mac-os-x-1082/

这样你就可以检查它的作用。默认网关包含“G”标志,但不应连接“I”标志(当 wifi 和 cell 都处于活动状态时,wifi 用于互联网连接 - 不使用 cell 网关并赋予“I”标志) 。

希望有帮助。

You need to read the routing table - basically the same way as "netstat -r" command does. The netstat implementation is opensource - it's in the package

network_cmds-396.6

at

http://www.opensource.apple.com/release/mac-os-x-1082/

so you can check what it does. The default gateway contains the "G" flag but shouldn't connect the "I" flag (when both wifi and cell are active, wifi is used for internet connection - the cell gateway is not used and is given the "I" flag).

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文