gpt4 book ai didi

iOS:outputStream 不会保持打开状态

转载 作者:行者123 更新时间:2023-11-28 20:18:41 25 4
gpt4 key购买 nike

好的,我正在实现 NSOutput 和输入流。当它们被初始化时,流打开并连接到我在我的 PC 上运行的 TCPServer,但随后似乎在初始化函数之外关闭一次,或者流可能没有正确存储。我不太确定,所以非常感谢任何帮助

在我的 .h 文件中:

@interface AVCamViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate, NSStreamDelegate> {
NSOutputStream *outputStream;
NSInputStream *inputStream;
}

@property (retain) NSInputStream *inputStream;
@property (retain) NSOutputStream *outputStream;

在我的 .m 文件中:

@synthesize inputStream, outputStream;

-(void)sendaResponse
{
NSString *response = [NSString stringWithFormat:@"Hello from Ipad Simulator"];
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
NSLog(@"%u", outputStream.streamStatus);
}

-(void)initNetworkCommunication
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"10.9.100.122", 80, &readStream, &writeStream);

NSInputStream *inputStream = (NSInputStream *) readStream;
NSOutputStream *outputStream = (NSOutputStream *) writeStream;

[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];


NSLog(@"%u and %u", inputStream.streamStatus, outputStream.streamStatus);
NSString *response = [NSString stringWithFormat:@"Hello from Mikes Ipad Simulator"];
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
NSLog(@"%u", outputStream.streamStatus);
}

-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
switch (eventCode) {
case NSStreamEventOpenCompleted:
NSLog(@"Stream Opened %u", outputStream.streamStatus);
break;

case NSStreamEventHasBytesAvailable:
if(aStream == inputStream)
{
uint8_t buffer [1024];
int len;

while ([inputStream hasBytesAvailable])
{
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if(len>0)
{
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];

if(nil != output)
{
NSLog(@"Server said: %@", output);
}
}
}
}
break;

case NSStreamEventErrorOccurred:
NSLog(@"Can not connect to the host!");
break;

case NSStreamEventEndEncountered:
if(aStream == inputStream)
NSLog(@"Ending input stream");
if(aStream == outputStream)
NSLog(@"Ending output stream");

[aStream close];
[aStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[aStream release];
aStream = nil;
break;

default:
break;
}
}

在 initNetworkConnection 中,模拟器连接到我的 TCPServer 并打开一个连接(streamstatus = 2)。收到消息“来自 Ipad 模拟器的问候”。但是后来我通过单击按钮调用了“sendaResponse”方法,然后 streamstatus = 0 并且“outputstream”似乎没有被初始化。我只是没有正确保留变量吗?

最佳答案

我假设您使用的是 ARC。删除此代码:

{
NSOutputStream *outputStream;
NSInputStream *inputStream;
}

@synthesize inputStream, outputStream;

这只会让您混淆属性和实例变量。拥有它的结果是您直接设置实例变量,并且您的流实例正在被 ARC 释放。

相反,一旦您删除了上面的代码,就可以使用 self.inputStreamself.outputStream 访问流。这将使属性正常工作并确保遵守您配置的“保留”规范。

您的原始代码也可能只是不尝试设置属性...您的配置应该是:

    NSInputStream *inputStream = (NSInputStream *) readStream;
NSOutputStream *outputStream = (NSOutputStream *) writeStream;

[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];

self.inputStream = inputStream;
self.outputStream = outputStream;

然后将属性更改为:

@property (strong) NSInputStream *inputStream;
@property (strong) NSOutputStream *outputStream;

关于iOS:outputStream 不会保持打开状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16844350/

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