我是套接字编程的新手,我试图跟踪通过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];

===============>>#1 票数: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

  ask by user3497411 translate from so

未解决问题?本站智能推荐: