在iPhone中使用getaddrinfo [英] getaddrinfo in iPhone

查看:213
本文介绍了在iPhone中使用getaddrinfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我准备小申请。当我重新启动iPhone 3G(使用3G互联网连接)并安装此应用程序时,getaddrinfo始终返回EAI_NONAME(8)。我关闭应用程序并运行Safari然后运行我的应用程序 - 所有工作。有什么问题?

I prepare small application. When I restart my iPhone 3G (with 3G internet connection) and install this application getaddrinfo always return EAI_NONAME(8). I close application and run Safari then run my application - all work. What's problem?

- (void)viewDidLoad {
    [super viewDidLoad];

    const char* hostname = "google.com";

    struct addrinfo hints, *res;
    int retval;

    memset (&hints, 0, sizeof (struct addrinfo));
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    retval = getaddrinfo (hostname, NULL, &hints, &res);
    if (retval == 0) {
        freeaddrinfo (res);
    }else if(retval == EAI_NONAME)
    {
        //noname
    }
}

来自Apple的回答:

Hello Pavel,

Hello Pavel,

我正在回应你的发现,试图使用BSD getaddrinfo功能在尝试获得只能进行无线广域网(WWAN)连接的连接时会出现问题(EDGE,3G)。您遇到的问题是,出于节省电池的原因,WWAN将在不需要网络服务时关闭。然后问题是,当需要网络服务时如何激活WWAN。

I'm responding regarding your finding that trying to use the BSD getaddrinfo function is problematic when trying to get a connection where only a Wireless Wide Area Network (WWAN) connection is possible (EDGE, 3G). The issue you've encountered is that for battery saving reasons, WWAN will shutdown when network services are not needed. The question then becomes, how to activate WWAN, when network services are required.

建立WWAN连接的官方支持机制是使用CFSocketStream API(或依赖API) - NSSocket,以及CFHTTPStream,CFFTPStream,NSURLRequest和NSURLConnection API)。这意味着正式,只支持TCP。此限制会影响所有基于UDP和BSD套接字的应用程序。使用BSD Connect呼叫不会触发iPhone启用WWAN。此限制也适用于WiFi连接下导致数据包传输的所有其他BSD功能。

The officially supported mechanism to establish a WWAN connection is to use the CFSocketStream API (or dependent API - NSSocket, as well as CFHTTPStream, CFFTPStream, NSURLRequest, and NSURLConnection API's). This means that officially, only TCP is supported. This limitation impacts all UDP and BSD Socket based applications. The use of a BSD Connect call will not trigger the iPhone to enable WWAN. This limitation also applies to all other BSD functions which under a WiFi connection would result in a packet transmission.

然而,在当前的iPhone OS下,一旦建立了WWAN连接,使用BSD套接字和CFSocket函数,将使WWAN连接保持活动状态。这使得基于UDP或BSD套接字的应用程序可以通过使用CFSocketStream API与TCP服务器(包括HTTP Web服务器)建立连接来建立WWAN连接。一旦连接处于活动状态,程序就可以像过去一样运行。这就是访问网络服务的Safari,Mail和其他Apple应用程序用于建立连接的方式。因此,当您启动Safari时,启动您的应用程序网络访问工作。使用Safari会导致iPhone建立WWAN连接。当您退出Safari时,WWAN不会自动关闭 - 它会在短时间内保持活动状态。如果启动应用程序,则WWAN服务处于活动状态,只要存在网络活动,您的应用程序将使WWAN保持活动状态。

However, under the current iPhone OS, once a WWAN connection is established, the use of BSD sockets and CFSocket functions, will keep the WWAN connection alive. This makes it possible for a UDP or BSD Socket based application to establish a WWAN connection by using the CFSocketStream API to establish a connection with a TCP server (including HTTP Web Servers). Once the connection is active, then the program can function as it has in the past. This is what Safari, Mail, and other Apple applications which access network services, uses to establish their connections. For this reason, when you launch Safari, then launch your application network access works. Using Safari causes the iPhone to establish a WWAN connection. When you quit Safari, WWAN does not automatically shutdown - it remains active for a short period of time. If you launch your application, WWAN services are active and your application will cause WWAN to remain active so long as there is network activity.

如果您的应用程序要使用TCP,对于网络,您可以修改代码以使用CFSocketStream或依赖API进行网络连接。当然,如果你有一个完整的BSD套接字功能库,对于想要很快将应用程序放到App Store上的人来说,这可能不是一个合理的解决方案。

If your application were to use TCP, for networking, then you could modify your code to use the CFSocketStream or dependent API, for networking. Of course, if you have a whole library of BSD socket functionality, this may not be a reasonable solution for someone who wants to get their application onto the App Store real soon.

....

推荐答案

这里可能发生的是 getaddrinfo()没有阻止等待DNS查找,这可能是一件好事,因为iPhone上的DNS查找可能需要很长时间。一旦你在其他地方完成了查找,它就会给你缓存的答案。

What's probably happening here is that getaddrinfo() is not blocking to wait for the DNS lookup, which is possibly a good thing since DNS lookups on iPhone can take a very long time. Once you've done the lookup elsewhere, it's giving you the cached answer.

看看 CFHost ,这是在iPhone上获取此类信息的首选方式。它支持异步回调,让您知道信息何时可用,或者您可以根据需要进行同步调用。

Take a look at CFHost, which is the preferred way to get this kind of information on iPhone. It supports asynchronous callbacks to let you know when the information is available, or you can make synchronous calls if you like.

这篇关于在iPhone中使用getaddrinfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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