我一直在尝试为我的ios应用程序启动并运行一个基本的tcp客户端,但遇到了一个似乎无法正常工作的块。
到目前为止,我可以连接,发送一个在服务器端接收到的消息,但是我的应用程序崩溃了。
客户h

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

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

客户
#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保留对其委托的assign/unsafe\u引用。如果在关闭和释放流之前释放了委托,则流将尝试对其现在已释放的委托调用方法,从而导致崩溃。解决方案是确保其他某个对象具有对客户端的强引用,从而阻止其早期释放,或者确保在释放其委托之前关闭并释放流。

10-08 07:20
查看更多