获取iPhone上当前Wi-Fi接入点的IP地址?

获取iPhone上当前Wi-Fi接入点的IP地址?,iphone,ip-address,local-network,Iphone,Ip Address,Local Network,我正在尝试从我的连接到计算机上运行在同一网络上的套接字服务器。如何获取运行套接字服务器的计算机的本地数据?\include #include <netinet/in.h> #include <arpa/inet.h> #include <net/if.h> #include <ifaddrs.h> - (NSString *)getIPAddress { NSString *address = @"error"; struct i

我正在尝试从我的连接到计算机上运行在同一网络上的套接字服务器。如何获取运行套接字服务器的计算机的本地数据?

\include
#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;
}
#包括 #包括 #包括 -(NSString*)getIPAddress { NSString*地址=@“错误”; struct ifaddrs*interfaces=NULL; 结构ifaddrs*临时地址=NULL; int成功=0; //检索当前接口-成功时返回0 success=getifaddrs(&interfaces); 如果(成功==0) { //循环浏览接口的链接列表 临时地址=接口; while(临时地址!=NULL) { 如果(临时地址->ifa地址->sa家庭==AF家庭) { //检查接口是否为en0,这是iPhone上的wifi连接 如果([[NSString stringWithUTF8String:temp_addr->ifa_name]是等效字符串:@“en0”]) { //从C字符串获取NSString 地址=[NSString stringWithUTF8String:inet_ntoa(((结构sockaddr_in*)temp_addr->ifa_addr)->sin_addr)]; } } 临时地址=临时地址->ifa\U下一步; } } //空闲内存 freeifaddrs(接口); 回信地址; }
您的服务器需要使用

然后,在iPhone上,您只需使用
gethostbyname
即可获取。您还有一个特定的API: