Idea: 1.gethostbyname (szname); Get host information structure
2.memcpy (&ip_addr,phot->h_addr_list[0],4); Remove the required 32-bit IP address from the host information structure IP_ADDR (binary)
3.inet_ntop (af_inet, &ip_addr, IP, sizeof (IP));//Convert a binary integer to a dotted decimal
#pragma mark Domain resolution IP
-(nsstring*) Getipaddressbyhostname: (nsstring*) Strhostname
{
Hostent is a structure that records information about the host, which records the host's information, including host name, alias, address type, address length, and address list.
struct hostent *gethostbyname (const char *name), the gethostbyname function resolves the IP address of the server according to the domain name, which returns a struct struct hostent
Const char* szname = [Strhostname utf8string];
struct hostent* phot;
@try
{
Phot = gethostbyname (szname);
}
@catch (NSException * e)
{
return nil;
}
struct IN_ADDR {
in_addr_t s_addr;
// };
The struct in_addr is used to represent a 32-bit IPV4 address.
The in_addr_t is typically a 32-bit unsigned int whose byte order is in Network order (NET Byte ordered), that is, the unsigned integer takes the big-endian byte order [1].
Each of these 8 bits represents a numeric value in an IP address bit.
For example 192.168.3.144 is 0xc0a80390, where C0 is 192, A8 is 168, 03 is 3, 90 is 144
When printing, you can call the Inet_ntoa () function to convert it to a char * type.
struct in_addr ip_addr;
if (phot)
{
memcpy (&ip_addr,phot->h_addr_list[0],4);
void *memcpy (void *dest, const void *SRC, size_t n);
Copies n bytes from the starting position of the memory address referred to by the source SRC to the beginning of the memory address referred to by the target dest
H_addr_list[0] 4 bytes, 8 bits per byte, here is an array, a domain name corresponds to multiple IP addresses, or a machine has multiple network cards locally
}
Else
{
return nil;
}
Char ip[20] = {0};
Inet_ntop (af_inet, &ip_addr, IP, sizeof (IP));//Convert a binary integer to a dotted decimal
nsstring* stripaddress = [NSString Stringwithutf8string:ip];
return stripaddress;
}
Knowledge Point Supplement:
/* Try
{//Here is try start, program starts catching exception
If an exception enters catch {} and then enters finally{}
If there is no exception, go directly to finally{}
Try End
}
catch (SqlException ex)
{
Catch has an exception to come into my place
return false;
}
Finally
{
It's going to come into my place anyway, and it's a good place to do some free resources.
You can take it out of here.
}*/
const char *inet_ntop (int af, const void *SRC, char *dst, socklen_t CNT);
This function transforms the network binary structure to the ASCII type address, the parameters function and Inet_pton the same, just one more parameter socklen_t cnt, he is pointing to the buffer DST size, avoid overflow, if the buffer is too small to store the value of the address, then return a null pointer , and errno is set to ENOSPC.
Dotted decimal (dotted decimal Notation) is all called dotted (dotted) decimal notation, which is the IPV4 IP address identification method. IPv4 uses four bytes to represent an IP address, and each byte is represented as 0~255 in decimal. Dotted decimal means an IP address with 4 digits from 0~255. such as 192.168.1.1.
IOS Domain name resolution IP (parsing)