读取ios中所有套接字的发送和接收字节

时间:2014-04-21 05:22:33

标签: ios objective-c sockets

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

1 个答案:

答案 0 :(得分:0)

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

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

  - (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