Im facing issue for creating a socket connection with an iPV6 using CFStreamCreatePairWithSocketToHost. But I'm able to create socket connection with IPV4 for the same port number.
Tried with all scenarios like adding http, https without http, adding between IPV6 address, Nothing worked for me.
The output coming for IPV6 is stream event 8 (Error code is 8) Ending up with NSStreamEventErrorOccurred in handleEvent method Below is the code, i used to create socket connection
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");
}
}
Try removing
[]
before the IPV6 and use this format :Hopefully this should work.