如何在Mac上的Objective-C中确定活动接口的网络掩码? [英] How to determine netmask of active interface in Objective-C on a Mac?

查看:91
本文介绍了如何在Mac上的Objective-C中确定活动接口的网络掩码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定Macbook所在的网络段,因为然后我想使用CFHost类在此段中扫描活动主机(基本IP扫描程序).因此,我需要活动接口的IP和网络掩码.这就是我获取IP的方式:

NSString *ipAddr = [[[NSHost currentHost] addresses] objectAtIndex:0];

但是我绝对不知道如何获得Netmask,所以我有点卡住了.尤其是因为我在Objective-C领域还很陌生,并且我也不了解普通C语言.我查看过CFHost,CFNetwork,NSHost和各种Google热门歌曲,但到目前为止,没有发现有用的东西.

作为最后的选择,我想可以进行某种系统调用,或者从文件中读取它(哪个?),但是如果可能的话,我想避免这种情况.

那么,如何将匹配的网络掩码与通过NSHost获得的IP相匹配?任何建议将不胜感激.

谢谢!

解决方案

您将必须使用系统配置API.请参阅《系统配置编程指南》 系统配置框架参考

I'm trying to determine the network segment my Macbook is in, because I then want to scan this segment for active hosts (basic IP scanner) by using the CFHost class. Therefore I need the IP and Netmask of my active interface. This is how I get the IP:

NSString *ipAddr = [[[NSHost currentHost] addresses] objectAtIndex:0];

But I have absolutely no idea how to get the Netmask, so I'm kind of stuck. Especially because I'm fairly new at Objective-C and because I also do not have extensive knowledge of plain C. I've looked at CFHost, CFNetwork, NSHost and various Google hits, but have found nothing useful, so far.

As a last resort I could do a system call of sorts, I suppose, or read it from a file (which one?), but I want to avoid that, if possible.

So, how do I get the matching netmask to the ip obtained through NSHost? Any suggestions would be greatly appreciated.

Thanks!

解决方案

You will have to use the System Configuration API. See System Configuration Programming Guidelines and System Configuration Framework Reference

这篇关于如何在Mac上的Objective-C中确定活动接口的网络掩码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆