1

Cocoa と cocoa touch で NSInput/Output ストリームをセットアップしようとしています。Mac と iPod を Wifi 経由で接続しようとしています。ただし、何があっても、常に接続拒否エラーが発生します。各デバイスのアドレスをハード コードしました (これは単なるテストであるため)。私は基本的に Apple のドキュメントに従っていましたが、役に立ちませんでした。私は何日もこれにこだわっています。どんな助けでも大歓迎です!

コードの Mac 側。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    CFReadStreamRef tempRead;
    CFWriteStreamRef tempWrite;

    NSHost *theHost = [NSHost hostWithAddress:@"10.0.1.2"];

    CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)([theHost address]), 80, &tempRead, &tempWrite);

    myInput = (__bridge NSInputStream *)(tempRead);
    myOutput = (__bridge NSOutputStream *)(tempWrite);

    [myOutput setDelegate:(id<NSStreamDelegate>) self];
    [myInput setDelegate:(id<NSStreamDelegate>) self];

    [myOutput setProperty: NSStreamSocketSecurityLevelNone forKey: NSStreamSocketSecurityLevelKey];
    [myInput setProperty: NSStreamSocketSecurityLevelNone forKey: NSStreamSocketSecurityLevelKey];

    [myOutput scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [myInput scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [myOutput open];
    [myInput open];
}
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
    NSLog(@"StreamEvent: %lu", streamEvent);
    if(theStream == myOutput)
    {
        if(streamEvent == NSStreamEventHasSpaceAvailable)
        {
            NSString *myString = @"Hello";

            uint8_t *string1 = (unsigned char *) [myString UTF8String];

            NSInteger written;

            written = [myOutput write: (const uint8_t *) string1 maxLength: strlen((const char *)string1)];
            return;
            }
        }

        if(theStream == myInput)
        {
            NSLog(@"Reading event: %li", streamEvent);
            return;
        }


        NSError *theError = [myOutput streamError];

    NSLog(@"BasicError: %@", [theError localizedDescription]);
}
-(void)dealloc
{
    [myOutput close];
}

iPhone

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
    CFReadStreamRef tempRead;
    CFWriteStreamRef tempWrite;

    NSHost *theHost = [NSHost hostWithAddress:@"10.0.1.2"];


    [NSStream getStreamsToHost: theHost port:80 inputStream:&inpStream outputStream:&outpStream];


    NSLog(@"Input and output: %@, %@", tempRead, tempWrite);

    myInput = (__bridge NSInputStream *)(tempRead);
    myOutput = (__bridge NSOutputStream *)(tempWrite);

    [myOutput setDelegate:(id<NSStreamDelegate>) self];
    [myInput setDelegate:(id<NSStreamDelegate>) self];

    //If they intercept my voice, woop dee doo, nobody cares!
    [myInput setProperty: NSStreamSocketSecurityLevelNone forKey: NSStreamSocketSecurityLevelKey];

    [myInput scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [myInput open];

    NSLog(@"DidFinnishLaunchingStatus: %u", [myInput streamStatus]);

    return YES;
}

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
    NSLog(@"TheEvent: %u", streamEvent);

    if(streamEvent == NSStreamEventHasBytesAvailable)
    {
        NSLog(@"Reading");

        uint8_t * read1 = NULL;

        unsigned int outputBytes = 0;

        outputBytes = [myInput read: read1 maxLength:1024];

        NSLog(@"outputBytes: %i", outputBytes);
        if(outputBytes)
        {
            NSLog(@"read1: %s", read1);
        }
        return;
    }
}
-(void)dealloc
{
    //Cleanup is tidyness.
    [myInput close];
}
4

1 に答える 1

0

どうやら、この方法で 2 つのデバイスを接続できるとは思えません。それらの1つはサーバーでなければなりません。現在、私はSocketKitを使用しています

https://github.com/unixpickle/SocketKit

しかし、より良い解決策は Cocoa ASyncSocket だと思います。(これは私が次にしようとしているものです)

于 2013-03-17T19:34:07.950 に答える