3

我一直在尝试为我的 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,然后我遇到了错误的访问崩溃。问题是我不知道它在访问时遇到了什么问题?

任何帮助将不胜感激!

4

1 回答 1

8

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

于 2013-10-03T17:19:39.300 回答