2013-03-17 39 views
1

我想在可可和可可触摸中设置一些NSInput/Output流。我试图通过Wifi在我的Mac和iPod之间建立连接。不管怎样,虽然我总是得到连接拒绝错误。我有每个设备的地址硬编码(因为它只是一个测试)。我基本上遵循苹果的文档无济于事。我一直坚持这几天。任何帮助将不胜感激!Objective C NSStream连接总是被拒绝

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]; 
} 
+0

我使用该链接的代码https://gist.github.com/thefifthcircuit/446256。我可以建议你看看这个 – meth 2013-03-17 01:28:13

+0

该代码适合你,我认为?我以前看过那个页面,但它看起来和我的完全一样,所以我没有多想。我会仔细看看它。顺便说一下,你的地址是什么?我使用的东西像10.0.1.5(本地连接)这是正确的吗? – Sequence 2013-03-17 01:38:59

+0

嗯,我没有太多有关地址的信息,但因为我知道一些地址不能被用户使用。他们属于系统。我用我的IP地址作为主机。 – meth 2013-03-17 01:42:11

回答

0

好吧,显然我不认为你可以连接两个设备这种方式。其中之一必须是服务器。目前我使用SocketKit

https://github.com/unixpickle/SocketKit

但我相信更好的解决办法是可可ASyncSocket。 (这是我将要尝试下一步)