本文轉載至 http://www.cocoachina.com/bbs/read.php?tid=142713&page=e&#a
- 分享型別:遊戲開發相關
#include <netdb.h>
#include <sys/socket.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;
}
#include <sys/socket.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;
}