iOS域名转解析为IP
来源:互联网 发布:linux安装压缩软件 编辑:程序博客网 时间:2024/04/29 21:16
域名解析为IP
这里介绍下小得技能点:
- (NSString *) realmToIP:(NSString *)hostName{
const char *host = [hostName UTF8String];
// Get host entry info for given host
struct hostent *remoteHostEnt = gethostbyname(host);
// Get address info from host entry
struct in_addr *remoteInAddr = (struct in_addr *) remoteHostEnt->h_addr_list[0];
// Convert numeric addr to ASCII string
char *sRemoteInAddr = inet_ntoa(*remoteInAddr);
return [NSString stringWithFormat:@"%s",sRemoteInAddr];
}
- (NSString*)getIPAddressByHostName:(NSString*)strHostName
{
const char* szname = [strHostName UTF8String];
struct hostent* phot ;
@try
{
phot = gethostbyname(szname);
}
@catch (NSException * e)
{
return nil;
}
struct in_addr ip_addr;
memcpy(&ip_addr,phot->h_addr_list[0],4);///h_addr_list[0]里4个字节,每个字节8位,此处为一个数组,一个域名对应多个ip地址或者本地时一个机器有多个网卡
char ip[20] = {0};
inet_ntop(AF_INET, &ip_addr, ip, sizeof(ip));
NSString* strIPAddress = [NSString stringWithUTF8String:ip];
return strIPAddress;
}
两种方法都能成功解析域名成IP这里介绍的是一个很方便的域名转IP的方法。在项目开发中可能会遇到需要将域名转IP上传信息。我也会将自己写的Demo上传供大家需要时GET。
- iOS域名转解析为IP
- 解析IP地址为主机域名
- ios中域名转换为ip地址
- ios中域名转换为ip地址
- iOS 使用gethostbyname() 解析域名得到IP
- IOS IPV6域名转IP
- 域名转化为IP
- ios之域名转IP和获取IP
- Perl 域名转换为IP
- Socket之iOS项目中的socket应用/IPV6 转换/域名转IP/解析socket接受的数据/心跳包
- 何为域名泛解析
- c#域名转ip
- linux 域名转ip
- 域名转IP
- 域名转IP例程
- Dns解析为ip
- 域名转换为IP地址示例
- c#中域名转换为IP地址
- Java中字节流和字符流的基本认识
- 正则表达式大全
- Swift学习之路 -- 协议、扩展和泛型
- HttpUrlConnection 网络请求工具类
- iOS 打印类型
- iOS域名转解析为IP
- VMware虚拟机中ubuntu系统用户密码忘记解决办法
- mysql大数据高并发处理
- events 事件
- JPEG图像的解压缩操作
- [从头学数学] 第06节 6~10的认识和加减法
- Android Studio中获取sha1证书指纹数据的方法
- yii 操作cookie
- 快速排序法