我面临使用CFStreamCreatePairWithSocketToHost与iPV6创建套接字连接的问题。 但我能够使用IPV4为相同的端口号创建套接字连接。
尝试添加http,不带http的https,添加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 :(得分:2)
尝试在IPV6之前删除[]
并使用以下格式:
NSString *url = [@"fe80::fe15:b4ff:feb7:102a" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
希望这应该有用。