2015-06-10 26 views
1

進出口面臨的問題創造了IPv6使用CFStreamCreatePairWithSocketToHost套接字連接IPV6套接字連接。 但是我可以爲IPV4創建與同一端口號的套接字連接。建立使用CFStreamCreatePairWithSocketToHost

試圖用如添加HTTP,HTTPS不含http所有場景,IPv6地址之間加入, 沒有爲我工作。

未來IPv6的輸出是 流事件8(錯誤代碼是8) 中handleEvent方法與NSStreamEventErrorOccurred結束了 下面是代碼,我用來創建套接字連接

CFReadStreamRef readStream; 
CFWriteStreamRef writeStream; 
NSString *url = [@"[fe80::fe15:b4ff:feb7:102a]" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
// NSString *url = @"13.61.14.130"; 
NSURL *myUrl = [NSURL URLWithString:url]; 

CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)myUrl.path, 82, &readStream, &writeStream); 

inputStream = (NSInputStream *)readStream; 
outputStream = (NSOutputStream *)writeStream; 
[inputStream setDelegate:self]; 
[outputStream setDelegate:self]; 
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
[inputStream open]; 
[outputStream open]; 




-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent { 

    NSLog(@"stream event %i", streamEvent); 
    [[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%u",streamEvent] message:[NSString stringWithFormat:@"stream event %i", streamEvent] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]; 
    switch (streamEvent) { 

     case NSStreamEventOpenCompleted: 
      NSLog(@"Stream opened"); 
      break; 
     case NSStreamEventHasBytesAvailable: 

      if (theStream == inputStream) { 

       uint8_t buffer[1024]; 
       int len; 

       while ([inputStream hasBytesAvailable]) { 
        len = [inputStream read:buffer maxLength:sizeof(buffer)]; 
        if (len > 0) { 

         NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]; 

         if (nil != output) { 

          NSLog(@"server said: %@", output); 
          [self messageReceived:output]; 

         } 
        } 
       } 
      } 
      break; 


     case NSStreamEventErrorOccurred: 

      NSLog(@"Can not connect to the host!"); 
      break; 

     case NSStreamEventEndEncountered: 

      [theStream close]; 
      [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
      [theStream release]; 
      theStream = nil; 

      break; 
     default: 
      NSLog(@"Unknown event"); 
    } 

} 
+0

注意,你不應該使用:除非你有IDE本身的問題[標籤的Xcode]標籤。 –

回答

2

嘗試移除[]在IPV6之前並且使用這種格式:

NSString *url = [@"fe80::fe15:b4ff:feb7:102a" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

希望這應該工作。