gpt4 book ai didi

ios - Objective C NSStream 连接总是被拒绝

转载 作者:可可西里 更新时间:2023-11-01 02:54:48 27 4
gpt4 key购买 nike

我正在尝试在 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];
}

最佳答案

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

https://github.com/unixpickle/SocketKit

但我相信更好的解决方案是 Cocoa ASyncSocket。 (这是我接下来要尝试的)

关于ios - Objective C NSStream 连接总是被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15456552/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com