iPhone에서 내 컴퓨터의 동일한 네트워크에서 실행중인 소켓 서버에 연결하려고합니다. 소켓 서버를 실행하는 컴퓨터의 로컬 IP address을 얻으려면 어떻게합니까?iPhone에서 현재 Wi-Fi 액세스 포인트의 IP 주소를 가져 옵니까?
3
A
답변
5
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <ifaddrs.h>
- (NSString *)getIPAddress
{
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// Retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0)
{
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL)
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
{
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;
}
0
서버가 mDNS protocol을 사용하여 브로드 캐스트해야합니다.
그러면 iPhone에서는 gethostbyname
을 사용하면 IP address을 얻을 수 있습니다. 또한 특정 API가 있습니다 : DNS Service Discovery C Reference.
가 작동하지 않습니다. 오류가 있습니다. – aryaxt