Accessibility with Address-Server AND Port - iOS 5

I am trying to check if the server is online or offline: I am facing a problem that it has a port when connected to it

My code at the moment is:

struct sockaddr_in address; address.sin_len = sizeof(address); address.sin_family = AF_INET; address.sin_port = htons(25667); address.sin_addr.s_addr = inet_addr("fr7.mooshroom.net"); Reachability *reachability = [Reachability reachabilityWithAddress:&address]; 

Please let me know what I am doing wrong. And please, do not connect me with other questions, I was looking, and none of them have what I am looking for.

+4
source share
2 answers

I fixed it now, I needed to put the line:
const char *serverIPChar = [serverIP cStringUsingEncoding:NSASCIIStringEncoding];
and replace "fr7.mooshroom.net" inside inet_addr with serverIPChar . Thank you anyway

+1
source

Basically, the inet_addr () function does not resolve the domain name for you. You need to give it an IP address (for example, 127.0.0.1).

To resolve the DNS name to IP address, you need to look at the standard gethostbyname () functions.

To clarify:

 struct hostent *host = gethostbyname("fr7.mooshroom.net"); if (host) { struct in_addr in; NSLog(@"HOST: %s" , host->h_name); while (*host->h_addr_list) { bcopy(*host->h_addr_list++, (char *) &in, sizeof(in)); NSLog(@"IP: %s", inet_ntoa(in)); } } 

Now, having said all this, are you sure that this will do what you want? The documentation for SCNetworkReachabilityRef does not imply:

http://developer.apple.com/library/ios/#documentation/SystemConfiguration/Reference/SCNetworkReachabilityRef/Reference/reference.html

"A remote host is considered accessible when a data packet sent by the application to the network stack can leave the local device. Accessibility does not guarantee that the data packet will indeed be received by the host."

+5
source

All Articles