watchOS 6 TCP Connection - Операция не может быть завершена. (ошибка kCFErrorDomainCFNetwork -72000)

Я разрабатываю автономное приложение watchOS 6 с Xcode и Objective-C. Это приложение должно подключаться к TCP-серверу. Если я запускаю приложение на симуляторе, оно работает, если я запускаю его на своих физических Apple Watch Series 4, я получаю следующую ошибку: операция не может быть завершена. (ошибка kCFErrorDomainCFNetwork -72000).

Вот код:

InterfaceController.h

#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>

@interface InterfaceController : WKInterfaceController <NSStreamDelegate>
{
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;

    NSInputStream   *inputStream;
    NSOutputStream  *outputStream;

    NSMutableArray  *messages;
}


@property (weak, nonatomic) IBOutlet WKInterfaceLabel *connectedLabel;
@property (weak, nonatomic) IBOutlet WKInterfaceLabel *dataReceivedLabel;



@end

InterfaceController.m

#import "InterfaceController.h"


@interface InterfaceController () {
    NSString* dataToSend;

    // Server Info
    NSString* ipAddress;
    NSString* port;
}

@end


@implementation InterfaceController

@synthesize connectedLabel;
@synthesize dataReceivedLabel;

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];

    ipAddress = @"192.168.0.68";
    port = @"12345";

    dataToSend = @"Hello TCP!";
    [connectedLabel setText:@"Disconnected"];

}

- (IBAction)sendMessage {
    NSString *response  = [NSString stringWithFormat:@"msg:%@", dataToSend];
    NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
    [outputStream write:[data bytes] maxLength:[data length]];
}

- (void) messageReceived:(NSString *)message {

    [messages addObject:message];

    [dataReceivedLabel setText:message];
    NSLog(@"%@", message);
}

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

    NSLog(@"stream event %lu", (unsigned long)streamEvent);

    switch (streamEvent) {

        case NSStreamEventOpenCompleted:
            NSLog(@"Stream opened");
            connectedLabel.text = @"Connected";
            break;
        case NSStreamEventHasBytesAvailable:

            if (theStream == inputStream)
            {
                uint8_t buffer[1024];
                NSInteger 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 NSStreamEventHasSpaceAvailable:
            NSLog(@"Stream has space available now");
            break;

        case NSStreamEventErrorOccurred:
             NSLog(@"%@",[theStream streamError].localizedDescription);
            break;

        case NSStreamEventEndEncountered:

            [theStream close];
            [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            connectedLabel.text = @"Disconnected";
            NSLog(@"close stream");
            break;
        default:
            NSLog(@"Unknown event");
    }
}

- (IBAction)connectToServer {
    NSLog(@"Setting up connection to %@ : %i", ipAddress, [port intValue]);

    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef) ipAddress, [port intValue], &readStream, &writeStream);

    messages = [[NSMutableArray alloc] init];

    [self open];
}

- (IBAction)disconnect {
     [self close];
}

- (void)open {

    NSLog(@"Opening streams.");

    outputStream = (__bridge NSOutputStream *)writeStream;
    inputStream = (__bridge NSInputStream *)readStream;

    [outputStream setDelegate:self];
    [inputStream setDelegate:self];

    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [outputStream open];
    [inputStream open];

    [connectedLabel setText:@"Connected"];
}


- (void)close {
    NSLog(@"Closing streams.");
    [inputStream close];
    [outputStream close];
    [inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream setDelegate:nil];
    [outputStream setDelegate:nil];
    inputStream = nil;
    outputStream = nil;

    [connectedLabel setText:@"Disconnected"];
}


@end

Вот Interface.storyboard:

0 ответов

Другие вопросы по тегам