ios Socket Stream问题

时间:2011-06-23 09:21:26

标签: iphone objective-c ios

我使用tcp连接使用ip和port连接服务器。我可以在上面写入和读取流。我的问题是,当我关闭服务器.app停止“EXC_BAD_ACCESS”有人可以帮助我吗?

这是连接代码:

-(void) connectToServerUsingStream:(NSString *)urlStr 
                            portNo: (uint) portNo {

    if (![urlStr isEqualToString:@""]) 
    {
        NSURL *website = [NSURL URLWithString:urlStr];
        if (!website) 
        {
            NSLog(@"%@ is not a valid URL");
            return;
        }
        else
        {
            [NSStream getStreamsToHostNamed:urlStr 
                                       port:portNo 
                                inputStream:&iStream
                               outputStream:&oStream];            
            [iStream retain];
            [oStream retain];

            [iStream setDelegate:self];
            [oStream setDelegate:self];

            [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                               forMode:NSDefaultRunLoopMode];
            [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                               forMode:NSDefaultRunLoopMode];
            [oStream open];

            [iStream open];            
        }
    }    
}

这是流事件委托:

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
    switch(eventCode) {
        case NSStreamEventHasBytesAvailable:
        {
            if (data == nil) 
            {
                data = [[NSMutableData alloc] init];
            }
            uint8_t buf[1024];
            unsigned int len = 0;
            len = [(NSInputStream *)stream read:buf maxLength:1024];
            if(len) 
            {    
                [data appendBytes:(const void *)buf length:len];
                int bytesRead;
                bytesRead += len;

            }
            else 
            {

                NSLog(@"No data.");
                return;
            }

            NSString *str = [[NSString alloc] initWithData:data 
                                                  encoding:NSUTF8StringEncoding];
            NSLog(@"From server: %@",str);
            [str release];
            [data release];        
            data = nil;
            break;
        } 
        case NSStreamEventErrorOccurred:
        {
            NSError *theError = [stream streamError];
            NSLog(@"Error reading stream! ,Error %i: %@",[theError code], [theError localizedDescription]);
            [self disconnect];
            [self connectToServerUsingStream:kHostIP portNo:kPort];
            break;
        }
        case NSStreamEventHasSpaceAvailable:
        {
            if(stream == oStream && !isDataSent)
            {
                isDataSent = YES;
                [self writeToServer:@"HI"];
            }
            break;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

你应该实现NSStreamEventEndEncountered案例并关闭/释放流。

答案 1 :(得分:0)

您收到了哪些日志消息,以便您可以看到代码的哪些部分被调用?

您是否尝试过设置一些断点,然后逐步查看确切的故障点,然后检查物体,以防您没有正确保留/释放?

到目前为止,您还采取了哪些其他调试步骤?

答案 2 :(得分:0)

尝试添加以下链接器标志:

OTHER_LDFLAGS = -lz -lxml2 -ltidy -ObjC

到您的项目。