如何从objective-c中的域名获取ipAddress和ipAddress的域名 [英] How to get Domain Name of ipAddress and ipAddress from Domain Name in objective-c

查看:259
本文介绍了如何从objective-c中的域名获取ipAddress和ipAddress的域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我已经通过这个问题

允许从域名获取IPAddress。客观c中有可能吗?如何?

I have gone through this question.
Java allows to get the IPAddress from a domain name. Is it possible in objective c ? How ?

第二个问题是如何通过使用它的IPAddress获取设备/机器的名称。例如我有一个ipAddress 192.168.0.74 =什么是设备名称?在目标c?如何?

The second question is How to get the name of device/machine by using it's IPAddress. Say for example I have an ipAddress 192.168.0.74 = What is the device name ? in Objective c ? How ?

提前感谢分享您的知识。

Thanks in advance for sharing your knowledge.

推荐答案

我不知道这是否是最好的方法,但它主要适用于我。我把StackOverflow的IP地址(69.59.196.211)放在了我的位置,但是我把它放到了一个Google的IP地址(210.55.180.158),而且它给了我回来 cache.googlevideo.com (对于所有结果,不只是第一个)。

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.

这篇关于如何从objective-c中的域名获取ipAddress和ipAddress的域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆