美文网首页
iOS获取链接地址host地址

iOS获取链接地址host地址

作者: jazzfly | 来源:发表于2021-01-20 09:21 被阅读0次

导入头文件#

include <netdb.h>

include <arpa/inet.h>

-(NSString *) getIPWithHostName:(const NSString *)hostName
{
  const char *hostN= [hostName UTF8String];
  struct hostent* phot;
  @try {
      phot = gethostbyname(hostN);
       
  }
  @catch (NSException *exception) {
      return nil;
  }
   
  struct in_addr ip_addr;
  memcpy(&ip_addr, phot->h_addr_list[0], 4);
  char ip[20] = {0};
  inet_ntop(AF_INET, &ip_addr, ip, sizeof(ip));
   
  NSString* strIPAddress = [NSString stringWithUTF8String:ip];
  return strIPAddress;
}

相关文章

网友评论

      本文标题:iOS获取链接地址host地址

      本文链接:https://www.haomeiwen.com/subject/eaihzktx.html