我正在尝试在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];
}
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 :(得分:0)
好吧,显然我认为你不能用这种方式连接两台设备。其中一个必须是服务器。目前我正在使用SocketKit
https://github.com/unixpickle/SocketKit
但我相信更好的解决方案是Cocoa ASyncSocket。 (这是我接下来要尝试的)