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];
}
我使用該鏈接的代碼https://gist.github.com/thefifthcircuit/446256。我可以建議你看看這個 – meth 2013-03-17 01:28:13
該代碼適合你,我認爲?我以前看過那個頁面,但它看起來和我的完全一樣,所以我沒有多想。我會仔細看看它。順便說一下,你的地址是什麼?我使用的東西像10.0.1.5(本地連接)這是正確的嗎? – Sequence 2013-03-17 01:38:59
嗯,我沒有太多有關地址的信息,但因爲我知道一些地址不能被用戶使用。他們屬於系統。我用我的IP地址作爲主機。 – meth 2013-03-17 01:42:11