【问题标题】:Establishing IPV6 socket connection using CFStreamCreatePairWithSocketToHost使用 CFStreamCreatePairWithSocketToHost 建立 IPV6 套接字连接
【发布时间】:2015-06-10 08:03:26
【问题描述】:

我面临使用 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");
    }

}

【问题讨论】:

  • 请注意,除非 IDE 本身存在问题,否则不应使用 xcode 标签。

标签: ios objective-c sockets cfsocket


【解决方案1】:

尝试在 IPV6 之前删除 [] 并使用此格式:

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

希望这应该可行。

【讨论】:

    猜你喜欢
    • 2011-07-18
    • 2015-11-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多