gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 19:40:32 26 4
gpt4 key购买 nike

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

最佳答案

#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;
}

关于iphone - 获取 iPhone 上当前 Wi-Fi 接入点的 IP 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3538365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com