如何以编程方式查找以太网的链接速度?

时间:2014-09-17 11:12:32

标签: objective-c macos ethernet

我想要一个帮助,因为我是Objective C的新手,我的查询是以编程方式查找“ EtherNet的链接速度”。 在这个我应该得到如果LAN(ehternet)没有连接它应该显示 0 或相应的链接速度值应该来。

1 个答案:

答案 0 :(得分:1)

我有一个非常粗略的想法,你告诉我。您可以进一步改进这一点,以保持准确性和性能。基本思想在下面的代码中描述,

#import "TestViewController.h"
#import <mach/mach.h>

@interface TestViewController()<NSURLConnectionDataDelegate>

@property (nonatomic, weak) UIView *view;
@property (nonatomic, assign) int contentLength;
@property (nonatomic, strong) NSMutableData *responseData;

@end

@implementation TestViewController{
  UIView * textInputView;
  u_int64_t startTime;
  u_int64_t endTime;
  __weak UIBarButtonItem *startItem;
  __weak UILabel *label;

}

- (void)viewDidLoad{
  [super viewDidLoad];
  UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Start" style:UIBarButtonItemStylePlain target:self action:@selector(startTest:)];
  self.navigationItem.rightBarButtonItem = item;
  startItem = item;

  UILabel *aLabel = [[UILabel alloc] init];
  [aLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
  aLabel.font = [UIFont systemFontOfSize:40.0];
  [self.view addSubview:aLabel];
  label = aLabel;
  aLabel.text = @"0";

  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:aLabel attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:aLabel attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
}

- (void)startTest:(UIBarButtonItem*)item{
  item.enabled = NO;
  startTime =  mach_absolute_time();
  self.responseData = [NSMutableData data];
  [self startDownload];
}

- (void)startDownload{
  NSURL *url = [NSURL URLWithString:@"http://download.thinkbroadband.com/20MB.zip"];
  NSURLConnection *connection = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self];
  [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  [connection start];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
  NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  NSDictionary *headers = [httpResponse allHeaderFields];
  self.contentLength = [headers[@"Content-Length"] intValue];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
  [self.responseData appendData:data];
  [UIView transitionWithView:label duration:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
    label.text = [NSString stringWithFormat:@"%0.2f MBPS", [self getMBPS]];
  } completion:nil];

}

- (float)getMBPS{
  endTime = mach_absolute_time();
  u_int64_t elapsedTime =  endTime - startTime;

  const int64_t kOneMillion = 1000 * 1000 ;
  static mach_timebase_info_data_t s_timebase_info;

  if (s_timebase_info.denom == 0) {
    (void) mach_timebase_info(&s_timebase_info);
  }

  float elapsedMilliseconds = (float)((elapsedTime * s_timebase_info.numer) / (kOneMillion * s_timebase_info.denom));
  float elapsedSeconds = elapsedMilliseconds / 1000;

  float kb = ([self.responseData length] * 8) / 1000000.0;
  float kbps = kb / elapsedSeconds;
  return kbps;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
  [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  startItem.enabled = YES;
  label.text = [NSString stringWithFormat:@"%0.2f MBPS", [self getMBPS] ];
}

@end