我正在尝试在 Cocoa 和 cocoa touch 中设置一些 NSInput/Output 流。我正在尝试通过 Wifi 在我的 Mac 和我的 iPod 之间建立连接。不过,无论如何,我总是会收到连接被拒绝的错误。我对每个设备的地址进行了硬编码(因为这只是一个测试)。我基本上按照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];
}
苹果手机
- (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];
}