#include <netdb.h>
#include <arpa/inet.h>
- (NSString*)hostnameToIPAddress:(NSString*)hostname
{
struct hostent *remoteHostEnt = gethostbyname([hostname UTF8String]);
struct in_addr *remoteInAddr = (struct in_addr *) remoteHostEnt->h_addr_list[0];
return [NSString stringWithUTF8String:inet_ntoa(*remoteInAddr)];
}
NSLog(@"IP: %@", [self hostnameToIPAddress:@"tw.yahoo.com"]);
NSLog(@"IP: %@", [self hostnameToIPAddress:@"tw.yahoo.com"]);
NSLog(@"IP: %@", [self hostnameToIPAddress:@"tw.yahoo.com"]);
若看到查到結果不一樣,主因是 DNS 提供的分流服務。
沒有留言:
張貼留言