0

Okay so I'm implementing a NSOutput and Input Streams. When they are initialized, the streams open and connect to my TCPServer I have running on my PC, but then appear to close once outside of the initializing function or maybe the streams aren't being stored properly. I'm not quite sure so any assistance is greatly appreciated

In my .h file:

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

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

In my .m file:

@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;
        }
    }

In the initNetworkConnection the simulator connects to my TCPServer and opens a connection(streamstatus = 2). Receives the message "Hello from Ipad Simulator". But then I have the "sendaResponse" method called from a button click which then streamstatus = 0 and "outputstream" doesn't appear to be initialized. Am I just not retaining the variable properly?

4

1 回答 1

3

我假设您正在使用ARC。删除此代码:

{
NSOutputStream *outputStream;
NSInputStream *inputStream;
}

@synthesize inputStream, outputStream;

它只是让您在属性和实例变量之间感到困惑。拥有它的结果是您直接设置实例变量,并且您的流实例正在被 ARC 释放。

相反,一旦您删除了上面的代码,请使用self.inputStream和访问流self.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;
于 2013-05-30T19:32:14.337 回答