Objective-C - 以编程方式确定 iPod touch 的 IP 地址

发布于 2024-07-27 03:18:18 字数 326 浏览 8 评论 0原文

我正在为几个 iPod 设备使用 Objective-C 进行编程,并且我想知道一些事情。 我正在开发一个利用服务器-客户端模型的应用程序,并且使用带有 C 套接字的 UDP 协议。 是否有一个类可以让我确定 iPod 设备的 IP 地址? 在谷歌搜索其他论坛后,我没有找到任何东西。 显然这个命令不起作用,但像 ipAddress = self.ip 这样的命令正是我的想法。 我正在设置多播 C 套接字,并且正在尝试做一种类似于 ping 命令的解决方法,这显然在 Objective-C 中不存在,或者据我所知(这是有限的,因为我只进行过编程)至少从今年夏天开始就在 Objective-C 中)。 有什么建议或技巧吗?

I'm programming in objective-C for several iPod devices and I was wondering about something. I'm developing an application that utilizes the server-client model and I'm using the UDP protocol with C sockets. Is there a class out there that allows me to determine the iPod devices IP address? After googling around other forums, I haven't found anything. Obviously this command wouldn't work, but something like ipAddress = self.ip is what I had in mind. I'm setting up multicast C sockets and I'm trying to do a workaround that resembles the ping command, which obviously doesn't exist in objective-C either or to my knowledge (which is limited, as I've only been programming in objective-C since the start of this summer) at least. Any advice or tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱你一生 2024-08-03 03:18:18

这段代码将通过循环接口来检索它。

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

This snippet of code will retrieve it by looping through the interfaces.

- (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; 
} 
旧街凉风 2024-08-03 03:18:18

你看到这个了吗? http://www.appsamuck.com/day4.html。 我认为正确的答案是在SDK中使用CFHost

编辑
该项目中的源代码似乎使用了以下代码,这使其成为完全无效的解决方案,除非 Apple 决定将 NSHost 放入 SDK 中。

-(NSString*)getAddress {  
    char iphone_ip[255];  
    strcpy(iphone_ip,"127.0.0.1"); // if everything fails  
    NSHost* myhost =[NSHost currentHost];  
    if (myhost)  
    {  
        NSString *ad = [myhost address];  
        if (ad)  
            strcpy(iphone_ip,[ad cStringUsingEncoding: NSISOLatin1StringEncoding]);  
    }  
    return [NSString stringWithFormat:@"%s",iphone_ip];   
}

Did you see this? http://www.appsamuck.com/day4.html. I think the right answer is to use CFHost in the SDK.

EDIT
It appears the source in that project is using the following code, which makes it a completely invalid solution unless Apple decides to put NSHost into the SDK.

-(NSString*)getAddress {  
    char iphone_ip[255];  
    strcpy(iphone_ip,"127.0.0.1"); // if everything fails  
    NSHost* myhost =[NSHost currentHost];  
    if (myhost)  
    {  
        NSString *ad = [myhost address];  
        if (ad)  
            strcpy(iphone_ip,[ad cStringUsingEncoding: NSISOLatin1StringEncoding]);  
    }  
    return [NSString stringWithFormat:@"%s",iphone_ip];   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文