I am preparing a small application. When I restart my iPhone 3G (with a 3G Internet connection) and install this application, getaddrinfo always returns EAI_NONAME (8). Close the application and launch Safari, then launch my application - everything works. What is the 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) {
Apple answer:
Hi Paul
I answer in relation to your conclusion that trying to use the BSAD getaddrinfo function is problematic when trying to get a connection where it can only connect to a wireless wide area network (WWAN) (EDGE, 3G). The problem you are facing is that to save battery, WWAN will shut down if network services are not needed. The question then becomes how to activate WWAN when network services are required.
The officially supported mechanism for creating a WWAN connection is to use the CFSocketStream API (or the dependent API NSSocket, as well as CFHTTPStream, CFFTPStream, NSURLRequest, and the NSURLConnection API). This means that only TCP is officially supported. This limitation affects all UDP and BSD Socket based applications. Using a BSD Connect call will not cause iPhone to enable WWAN. This restriction also applies to all other BSD functions that, under a WiFi connection, will result in packet transmission.
However, under the current iPhone OS, when the WWAN connection is established, the use of BSD sockets and CFSocket functions will support the WWAN connection. This allows you to use a UDP or BSD Socket application to establish a WWAN connection using the CFSocketStream API to establish a connection to a TCP server (including HTTP HTTP servers). Once the connection is active, the program can function in the same way as in the past. This is what Safari, Mail, and other Apple applications that access network services use to establish their connections. For this reason, when you start Safari, access to the application network is started. Using Safari forces iPhone to establish a WWAN connection. When you leave Safari, WWAN does not turn off automatically - it remains active for a short period of time. If you run the application, WWAN services will be active, and your application will cause WWAN to remain active as long as there is network activity.
If your application was supposed to use TCP to connect to the network, you can change your code to use the CFSocketStream or dependent API for networking. Of course, if you have a whole library of BSD socket functions, this might not be the smart solution for those who want to get their app on the App Store soon.
....