gpt4 book ai didi

objective-c - 写入后 NSOutputStream 因访问错误而崩溃(Objective-c)

转载 作者:搜寻专家 更新时间:2023-10-30 19:57:48 26 4
gpt4 key购买 nike

我一直在尝试为我的 iOS 应用程序启动并运行一个基本的 TCP 客户端,但遇到了一个我似乎无法解决的问题。

到目前为止,我可以连接,发送一条在服务器端收到的消息,但随后我的应用程序崩溃了。

客户端.h

#import <Foundation/Foundation.h>
@interface Client : NSObject <NSStreamDelegate>
{
NSInputStream *inputStream;
NSOutputStream *outputStream;
}

-(void)initNetworkCommunication;
-(void)send:(NSString*)message;
@end

客户端.m

#import "Client.h"

@implementation Client

- (void)initNetworkCommunication {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"10.0.1.51", 7769, &readStream, &writeStream);
inputStream = ( NSInputStream *)CFBridgingRelease(readStream);
outputStream = ( NSOutputStream *)CFBridgingRelease(writeStream);

[inputStream setDelegate:self];
[outputStream setDelegate:self];

[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

[inputStream open];
[outputStream open];
}

- (void)send:(NSString*)message
{
NSData *data = [[NSData alloc] initWithData:[message dataUsingEncoding:NSUTF8StringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
}

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
NSLog(@"stream event %i", streamEvent);

switch (streamEvent) {

case NSStreamEventOpenCompleted:
NSLog(@"Stream opened");
break;

case NSStreamEventHasBytesAvailable:
if (theStream == 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:
NSLog(@"End Encountered!");
[theStream close];
[theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
theStream = nil;
break;

default:
NSLog(@"Unknown event");
}
}

@end

控制台的输出是

2013-10-03 17:01:38.542 MMORPG[6076:70b] stream event 1
2013-10-03 17:01:38.543 MMORPG[6076:70b] Stream opened
2013-10-03 17:01:43.495 MMORPG[6076:70b] stream event 4
2013-10-03 17:01:43.495 MMORPG[6076:70b] Unknown event

看起来,我的消息已发送,我收到了流事件 #4,然后我遇到了错误的访问崩溃。问题是我不知道访问有什么问题?

任何帮助将不胜感激!

最佳答案

问题是 NSStream 保留了对其委托(delegate)的 assign/unsafe_unretained 引用。如果委托(delegate)在流关闭和释放之前被释放,流将尝试调用其现在已解除分配的委托(delegate)的方法,从而导致崩溃。解决方案是要么确保某些其他对象对客户端具有强引用,以防止其提前解除分配,要么确保在其委托(delegate)被解除分配之前关闭并释放流。

关于objective-c - 写入后 NSOutputStream 因访问错误而崩溃(Objective-c),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19164033/

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