<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">这段时间公司iOS的网络测试的项目。</span>
首先,对我最不好做的模块Ping,这网上找了很久的资料都指向SimplePing这个源代码,SimplePing是由Apple提供的官方代码,官方下载地址:SimplePing,这里有一个对SimplePing进行封装的Demo,但是这个demo只能告诉用户ping的结果,即ping成功还是Ping失败了,不能像在Mac的终端和win的CMD命令行中显示ping的详情结果,所以并不完美。由于现在对ICMP并不熟悉,不知道怎么通过ICMP来计算这些参数,所以昨天晚上找资料到凌晨3点,在CocoaChina代码区,终于看到一个demo,心存侥幸地运行了一下,运行结果好极了,跟我想要的基本一致,原文链接,demo相当完美。demo下载地址https://github.com/lovesunstar/STPingTest。不过demo里面用到了作者自己的框架需要自己添加到工程中去,,并且还需要导入一个系统库libz.1.2.5,也需要手动导入工程。
其次,网络下载速度,比较简单。用AFNetWork中的AFURLConnectionOperation
- (void)setDownloadProgressBlock:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))block; 方法,在block中计算每秒钟的下载速度。
在计算速度的时候需要自己写一个downTask类,负责计算、记录每一秒(左右)的速度。downTask主要代码<a target=_blank href="http://doc.okbase.net/conslee/archive/126370.html">点击打开链接</a> 或 http://doc.okbase.net/conslee/archive/126370.html
<span style="font-family: Arial, Helvetica, sans-serif;">connectionOperation = [[AFURLConnectionOperation alloc] initWithRequest:request];</span>
[connectionOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
// NSLog(@"bytesRead:%zi", bytesRead);
// NSLog(@"totalBytesRead:%zi", totalBytesRead);
// NSLog(@"totalBytesExpectedToRead:%zi", totalBytesExpectedToRead);
weakSelf.downTask.totalReadPeriod += bytesRead;
weakSelf.downTask.totalRead += bytesRead;
NSDate *currentDate = [NSDate date];
if ([currentDate timeIntervalSinceDate:weakSelf.downTask.oldDatePeriod] >= 1) {
double speed = [weakSelf.downTask getSpeedWithDate:currentDate];
[weakSelf.gaugeView setGaugeValue:speed animation:YES];
NSString *unit = nil;
if (speed > RATIO) {
unit = @"M";
speed = speed / RATIO;
}
else {
unit = @"KB";
speed = speed;
}
NSLog(@"current speed:%f %@", speed, unit);
weakSelf.labSpeed.text = [NSString stringWithFormat:@"%.2f %@", speed, unit];
//NSLog(@"totalBytesRead:%zi", totalBytesRead);
}
}];
来源:CSDN
作者:yingms
链接:https://blog.csdn.net/yingms/article/details/46238407