- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我在 iPhone 应用程序中使用 Mobile snmp++ 库 ( https://github.com/Zchander/mobile-snmp-plusplus/tree/master/Mobile%20SNMP%2B%2B ) 使用 swift 语言扫描设备。
Mobile Snmp++ 库是用 Objective-C 编写的,带有 Bridging header ,我可以在我的 swift 项目中使用这个库,它运行良好。
假设我需要扫描特定 IP 地址范围内的设备170.23.45.0 至 170.23.45.255。为此,我使用 XISMobile_SNMP_PP.mm 中的 getoid 函数 ( https://github.com/Zchander/mobile-snmp-plusplus/blob/master/Mobile%20SNMP%2B%2B/XISMobile_SNMP_PP.mm )
要从单个 IP 地址获得响应,每次响应大约需要 1-2 秒。因此,为了减少时间,我按照以下链接中的建议使用多线程(自从我们一次最多只有 20 个线程将在 iphone 中使用该应用程序),从 0 到 255 的完整扫描需要 20 秒。我需要将这段时间减少到 5 秒左右。 optimized way to search a device ip address within a range in iphone
问题:每次搜索开始时,getoid 函数都会打开套接字并发送数据,然后获得响应并关闭套接字。那么我们不能保持套接字打开,扫描所有 ip 地址并得到它的响应并最后关闭套接字吗?我需要将搜索时间从 20 秒减少到 5 秒,那么我们如何在 getoid 函数中做到这一点。
- (NSDictionary *)getOid:(NSString *)oid
address:(NSString *)hostAddress
snmpVersion:(uint)version
remotePort:(NSNumber *)aPort
withCommunity:(NSString *)community
retry:(uint)retries
timeout:(uint)timeout
error:(NSError * __autoreleasing*)error
{
int status;
uint l_retries;
uint l_timeout;
NSNumber *localPort;
snmp_version snmpVersion = version1;
OctetStr snmpCommunity([community UTF8String]);
if ( aPort == nil ) {
localPort = [NSNumber numberWithInteger:161];
} else localPort = aPort;
if ( retries > 100 ) {
l_retries = 100;
} else l_retries = retries;
if ( timeout < 100 ) {
l_timeout = 100;
} else if ( timeout > 500 ) {
l_timeout = 500;
} else l_timeout = timeout;
switch ( version ) {
case 1:
snmpVersion = version1;
break;
case 2:
snmpVersion = version2c;
break;
default:
snmpVersion = version1;
break;
}
// Generate a SNMP++ generic address
UdpAddress udpAddress([hostAddress UTF8String]);
// Check if it is a valid address, if we got an invalid address
// we return a 'nil' dictionary and an error code
if ( !udpAddress.valid() ) {
*error = [self constructError:ERR_INVALID_DESTINATION];
#ifdef DEBUG
NSLog(@"ERROR SNMPController (getOid:hostAddress:oid:snmpVersion:remotePort:withCommunity:retry:timeout:error:)");
NSLog(@"ERROR SNMP++ Invalid host address or IP: %@", hostAddress);
NSLog(@"ERROR ====================");
#endif
return nil;
}
// Check if we got a valid Oid, otherwise use sysDescr
Oid localOid([oid UTF8String]);
if ( !localOid.valid() ) {
#ifdef DEBUG
NSLog(@"ERROR SNMPController (getOid:hostAddress:oid:snmpVersion:remotePort:withCommunity:retry:timeout:error:)");
NSLog(@"ERROR SNMP++ We got an invalid Oid (%@), we are using sysDescr for now (.1.3.6.1.2.1.1.1.0)", oid);
NSLog(@"ERROR ====================");
#endif
Oid localOid("1.3.6.1.2.1.1.1.0");
}
// Create the SNMP session
Snmp snmp(status, 0, (udpAddress.get_ip_version() == Address::version_ipv6));
if ( status != SNMP_CLASS_SUCCESS ) {
#ifdef DEBUG
NSLog(@"ERROR SNMPController (getOid:hostAddress:oid:snmpVersion:remotePort:withCommunity:retry:timeout:error:)");
NSLog(@"ERROR SNMP++ Could not create session: %s", snmp.error_msg(status));
NSLog(@"ERROR ====================");
#endif
*error = [self constructError:ERR_NO_SNMP_SESSION];
return nil;
}
// We are ready to build the SNMP++ object we need
Pdu pdu; // construct a Pdu object
Vb vb; // construct a Vb object
vb.set_oid(localOid); // set the Oid portion of the Vb
pdu += vb; // add the vb to the Pdu
// Set the port
udpAddress.set_port([localPort integerValue]);
CTarget ctarget(udpAddress); // Make a target using the address
ctarget.set_version(snmpVersion); // Set the SNMP version
ctarget.set_retry(l_retries); // Set the number of retries
ctarget.set_timeout(l_timeout); // Set the timeout for the request
ctarget.set_readcommunity(snmpCommunity); // Set the read community name
// Issue the request, in blocked mode
#ifdef DEBUG
NSLog(@"DEBUG SNMPController (getOid:hostAddress:oid:snmpVersion:remotePort:withCommunity:retry:timeout:error:)");
NSLog(@"DEBUG SNMP++ GET to %@ (oid: %@) with version %d on Port: %d using community %@ with retries %d and timeout %d", hostAddress, oid, version, [aPort integerValue], community, retries, timeout);
NSLog(@"DEBUG SNMP++ What is the community we are sending.... %s", snmpCommunity.get_printable());
NSLog(@"DEBUG ====================");
#endif
SnmpTarget *target;
target = &ctarget;
status = snmp.get(pdu, *target);
NSMutableDictionary *resultsDict = [[NSMutableDictionary alloc] init];
if ( status == SNMP_CLASS_SUCCESS ) {
pdu.get_vb(vb, 0);
#ifdef DEBUG
NSLog(@"DEBUG SNMPController (getOid:hostAddress:oid:snmpVersion:remotePort:withCommunity:retry:timeout:error:)");
NSLog(@"DEBUG SNMP++ -- Oid: %s", vb.get_printable_oid());
NSLog(@"DEBUG SNMP++ -- Value: %s", vb.get_printable_value());
#endif
// Add the result(s) to the resultsDict
[resultsDict setObject:[NSString stringWithUTF8String:vb.get_printable_value()] forKey:[NSString stringWithUTF8String:vb.get_printable_oid()]];
if ( (vb.get_syntax() == sNMP_SYNTAX_ENDOFMIBVIEW) ||
(vb.get_syntax() == sNMP_SYNTAX_NOSUCHINSTANCE) ||
(vb.get_syntax() == sNMP_SYNTAX_NOSUCHOBJECT)) {
NSLog(@"ERROR SNMP++ Exception found: %lu", vb.get_syntax());
} else {
NSLog(@"ERROR SNMP++ GET Error: %s (%d)", snmp.error_msg(status), status);
}
#ifdef DEBUG
NSLog(@"DEBUG ====================");
#endif
}
// Make sure error is nil!
*error = nil;
return ( resultsDict != nil ) ? [NSDictionary dictionaryWithDictionary:resultsDict] : nil;
}
最佳答案
您正在使用的底层 C++ 库支持在收到响应时使用回调。如果您更新 Objective-C 包装器以使用这种方法而不是线程阻塞 getOid 函数,您将看到更好的性能,并且您将只需要使用 1 或 2 个线程。
我会看看
int Snmp::get(Pdu &pdu, const SnmpTarget &target,
常量 snmp_callback 回调,
const void * callback_data)
在 uxsnmp.cpp
中。您甚至可以包装它以使用 Obj-C block 。这就是您要获得性能的地方。
只要您使用异步库 Hook ,像这样触发大量 SNMP-over-UDP 查询是非常可行的,我使用 Ruby 的 EventMachine 以相同的方式进行 DNS 查询得到了很好的结果 - 我得到了约 1500 个查询在 3-5 秒内完成!!
关于ios - 优化 snmp 库以在 iphone 中搜索设备 ip 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34191820/
IO 设备如何知道属于它的内存中的值在memory mapped IO 中发生了变化? ? 例如,假设内存地址 0 专用于保存 VGA 设备的背景颜色。当我们更改 memory[0] 中的值时,VGA
我目前正在开发一个使用Facebook sdk登录(通过FBLoginView)的iOS应用。 一切正常,除了那些拥有较旧版本的facebook的人。 当他们按下“使用Facebook登录”按钮时,他
假设我有: this - is an - example - with some - dashesNSRange将使用`rangeOfString:@“-”拾取“-”的第一个实例,但是如果我只想要最后
Card.io SDK提供以下详细信息: 卡号,有效期,月份,年份,CVV和邮政编码。 如何从此SDK获取国家名称。 - (void)userDidProvideCreditCardInfo:(Car
iOS 应用程序如何从网络服务下载图片并在安装过程中将它们安装到用户的 iOS 设备上?可能吗? 最佳答案 您无法控制应用在用户设备上的安装,因此无法在安装过程中下载其他数据。 只需在安装后首次启动应
我曾经开发过一款企业版 iOS 产品,我们公司曾将其出售给大型企业,供他们的员工使用。 该应用程序通过 AppStore 提供,企业用户获得了公司特定的配置文件(包含应用程序配置文件)以启用他们有权使
我正在尝试将 Card.io SDK 集成到我的 iOS 应用程序中。我想为 CardIO ui 做一个简单的本地化,如更改取消按钮标题或“在此保留信用卡”提示文本。 我在 github 上找到了这个
我正在使用 CardIOView 和 CardIOViewDelegate 类,没有可以设置为 YES 的 BOOL 来扫描 collectCardholderName。我可以看到它在 CardIOP
我有一个集成了通话工具包的 voip 应用程序。每次我从我的 voip 应用程序调用时,都会在 native 电话应用程序中创建一个新的最近通话记录。我在 voip 应用程序中也有自定义联系人(电话应
iOS 应用程序如何知道应用程序打开时屏幕上是否已经有键盘?应用程序运行后,它可以接收键盘显示/隐藏通知。但是,如果应用程序在分屏模式下作为辅助应用程序打开,而主应用程序已经显示键盘,则辅助应用程序不
我在模拟器中收到以下错误: ImageIO: CGImageReadSessionGetCachedImageBlockData *** CGImageReadSessionGetCachedIm
如 Apple 文档所示,可以通过 EAAccessory Framework 与经过认证的配件(由 Apple 认证)进行通信。但是我有点困惑,因为一些帖子告诉我它也可以通过 CoreBluetoo
尽管现在的调试器已经很不错了,但有时找出应用程序中正在发生的事情的最好方法仍然是古老的 NSLog。当您连接到计算机时,这样做很容易; Xcode 会帮助弹出日志查看器面板,然后就可以了。当您不在办公
在我的 iOS 应用程序中,我定义了一些兴趣点。其中一些有一个 Kontakt.io 信标的名称,它绑定(bind)到一个特定的 PoI(我的意思是通常贴在信标标签上的名称)。现在我想在附近发现信标,
我正在为警报提示创建一个 trigger.io 插件。尝试从警报提示返回数据。这是我的代码: // Prompt + (void)show_prompt:(ForgeTask*)task{
您好,我是 Apple iOS 的新手。我阅读并搜索了很多关于推送通知的文章,但我没有发现任何关于 APNS 从 io4 到 ios 6 的新更新的信息。任何人都可以向我提供 APNS 如何在 ios
UITabBar 的高度似乎在 iOS 7 和 8/9/10/11 之间发生了变化。我发布这个问题是为了让其他人轻松找到答案。 那么:在 iPhone 和 iPad 上的 iOS 8/9/10/11
我想我可以针对不同的 iOS 版本使用不同的 Storyboard。 由于 UI 的差异,我将创建下一个 Storyboard: Main_iPhone.storyboard Main_iPad.st
我正在写一些东西,我将使用设备的 iTunes 库中的一部分音轨来覆盖 2 个视频的组合,例如: AVMutableComposition* mixComposition = [[AVMutableC
我创建了一个简单的 iOS 程序,可以顺利编译并在 iPad 模拟器上运行良好。当我告诉 XCode 4 使用我连接的 iPad 设备时,无法编译相同的程序。问题似乎是当我尝试使用附加的 iPad 时
我是一名优秀的程序员,十分优秀!