【问题标题】:Reading sent and received bytes of all sockets in ios读取ios中所有套接字的发送和接收字节
【发布时间】:2014-04-21 14:30:18
【问题描述】:

我是套接字编程的新手。我正在尝试跟踪通过我的 iPhone 中打开的所有套接字发送和接收的字节。 iOS 中存在哪些数据结构,提供有关这些的信息?我尝试了下面一个 SO 问题中发布的代码,但它提供了上次重启时间的数据。我想列出在我的 iPhone 中建立的所有套接字并跟踪通过每个套接字发送和接收的字节

    - (NSArray *)getDataCounters {
    BOOL   success;
    struct ifaddrs *addrs;
    const struct ifaddrs *cursor;
    const struct if_data *networkStatisc; 

int WiFiSent = 0;
int WiFiReceived = 0;
int WWANSent = 0;
int WWANReceived = 0;

NSString *name=[[[NSString alloc]init]autorelease];

success = getifaddrs(&addrs) == 0;
if (success) 
{
    cursor = addrs;
    while (cursor != NULL) 
    {
        name=[NSString stringWithFormat:@"%s",cursor->ifa_name];
        NSLog(@"ifa_name %s == %@\n", cursor->ifa_name,name);
        // names of interfaces: en0 is WiFi ,pdp_ip0 is WWAN 

        if (cursor->ifa_addr->sa_family == AF_LINK) 
        {
            if ([name hasPrefix:@"en"]) 
            {
                networkStatisc = (const struct if_data *) cursor->ifa_data;
                WiFiSent+=networkStatisc->ifi_obytes;
                WiFiReceived+=networkStatisc->ifi_ibytes;
                NSLog(@"WiFiSent %d ==%d",WiFiSent,networkStatisc->ifi_obytes);
                NSLog(@"WiFiReceived %d ==%d",WiFiReceived,networkStatisc->ifi_ibytes);
            }

            if ([name hasPrefix:@"pdp_ip"]) 
            {
                networkStatisc = (const struct if_data *) cursor->ifa_data;
                WWANSent+=networkStatisc->ifi_obytes;
                WWANReceived+=networkStatisc->ifi_ibytes;
                NSLog(@"WWANSent %d ==%d",WWANSent,networkStatisc->ifi_obytes);
                NSLog(@"WWANReceived %d ==%d",WWANReceived,networkStatisc->ifi_ibytes);
            } 
        }

        cursor = cursor->ifa_next;
    }

    freeifaddrs(addrs);
}       

return [NSArray arrayWithObjects:[NSNumber numberWithInt:WiFiSent], [NSNumber numberWithInt:WiFiReceived],[NSNumber numberWithInt:WWANSent],[NSNumber numberWithInt:WWANReceived], nil];

【问题讨论】:

    标签: ios objective-c sockets


    【解决方案1】:

    我认为你应该从基础开始。

    这就是我们为输入和输出流打开套接字的方式。

      - (IBAction)searchForSite:(id)sender
    
           {
              NSString *urlStr = [sender stringValue];
    
                if (![urlStr isEqualToString:@""]) {
    
                NSURL *website = [NSURL URLWithString:urlStr];
                 if (!website) {
                NSLog(@"%@ is not a valid URL");
                  return;
           }   
    
    
        CFReadStreamRef readStream;
    
        CFWriteStreamRef writeStream;
    
        CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[website host], 80, &readStream, &writeStream);
    
        NSInputStream *inputStream = (__bridge_transfer NSInputStream *)readStream;
        NSOutputStream *outputStream = (__bridge_transfer NSOutputStream *)writeStream;
        [inputStream setDelegate:self];
        [outputStream setDelegate:self];
        [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [inputStream open];
        [outputStream open];
    
        /* Store a reference to the input and output streams so that
           they don't go away.... */
        ...
    }
    

    }

    this is the delegate method,whick will called every time when you will read or write to socket.
    
    - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
    
     switch (streamEvent) {
    
        case NSStreamEventOpenCompleted:
            NSLog(@"Stream opened");
            break;
    
                case NSStreamEventHasBytesAvailable:
                      break;   
    
               case NSStreamEventHasSpaceAvailable 
    
                      break;   
    
    
        case NSStreamEventErrorOccurred:
            NSLog(@"Can not connect to the host!");
            break;
    
        case NSStreamEventEndEncountered:
            break;
    
        default:
            NSLog(@"Unknown event");
    }
    

    }

    有关更多信息,请参阅链接。 https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Streams/Articles/NetworkStreams.html#//apple_ref/doc/uid/20002277-BCIDFCDI

    http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server

    【讨论】:

      猜你喜欢
      • 2020-11-16
      • 2011-07-11
      • 2010-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多