域名转IP地址

Objective-C

Posted by YiMiTuMi on August 15, 2023

域名转IP地址

-(NSString *)queryIpWithDomain:(NSString *)domain
{
    struct hostent *hs;
    struct sockaddr_in server;
    if ((hs = gethostbyname([domain UTF8String])) != NULL)
    {
        server.sin_addr = *((struct in_addr*)hs->h_addr_list[0]);
        return [NSString stringWithUTF8String:inet_ntoa(server.sin_addr)];
    }
    return nil;
}

当输入的原本就是IP地址则不做转换,现只支持IPV4的地址转换。