NSInputStream不接收所有数据

时间:2015-07-24 04:44:25

标签: ios objective-c sockets nsstream

当我从服务器接收数据时出现错误(数据只是字符串)。

例如,

服务器发送数据(15000字节)
我的iPhone收到数据(7878字节)

所以我尝试搜索,我知道方法是答案。

但我无法应用我的代码..

我猜NSMutableData是提示..

怎么做..

这是我的代码。

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {

NSLog(@"stream event %lu", (unsigned long)streamEvent);

switch (streamEvent) {

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

        if(_connectServer)
        {
            [_connectServer invalidate];
            _connectServer = nil;
        }

        break;
    case NSStreamEventHasBytesAvailable:

        if (theStream == inputStream)
        {
            if(!_serverOpen)
            {
                uint8_t buffer[1024];

                int len;

                while ([inputStream hasBytesAvailable])
                {
                    len = (int)[inputStream read:buffer maxLength:sizeof(buffer)];

                    if (len > 0)
                    {
                        NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding];

                        if (nil != output)
                        {

                            [self receivedServerData:output];

                            if(!_serverOpen)
                            {
                                [self initNetworkCommunicationWithIpPort:output];
                            }

                        }
                    }
                }
            }

            else
            {
                uint8_t buffer[20];

                int len;

                while ([inputStream hasBytesAvailable])
                {
                    len = (int)[inputStream read:buffer maxLength:sizeof(buffer)];

                    NSString* strBuffer = [[NSString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding];
                    NSLog(@"STRBUFFER %@", strBuffer);

                    strBuffer = [strBuffer substringFromIndex:7];

                    uint8_t dataBuffer[[strBuffer intValue]];

                    NSLog(@"databuffer %lu", sizeof(dataBuffer));

                    int lenBuffer = [inputStream read:dataBuffer maxLength:sizeof(dataBuffer)];

                    NSLog(@"lenBuffer %d", lenBuffer);

                    int position = 0 ;

                    if (lenBuffer > 0)
                    {
                        NSString *output = [[NSString alloc] initWithBytes:dataBuffer length:lenBuffer encoding:NSUTF8StringEncoding];

                        if (nil != output)
                        {
                            [self receivedServerData:output];

                            if(!_serverOpen)
                            {
                                [self initNetworkCommunicationWithIpPort:output];
                            }
                        }
                    }
                }
            }
        }
        else
        {
            NSLog(@"333");
        }
        break;

    case NSStreamEventHasSpaceAvailable:
    {
    }
        break;

    case NSStreamEventErrorOccurred:
        NSLog(@"Can not connect to the host!");
        _serverOpen = NO;

        break;

    case NSStreamEventEndEncountered:
        [theStream close];
        [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        theStream = nil;

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

2 个答案:

答案 0 :(得分:1)

正如@Manav声明的那样,您可以期望以几个块的形式接收流,只需将每个块附加到NSMutableData,直到获得NSStreamEventEndEncountered

答案 1 :(得分:0)

如果您有权访问发送数据的服务器,最简单的方法是将前4个字节设为int,表示数据的长度。然后解析数据中的前4个字节并读取长度。然后,当输入流上有可用空间时,如果您有足够的长度,则将字节附加到NSMutableData属性,直到接收器具有您在开始时获得的length。一旦完成,然后立即将其转换为字符串。由于网络延迟问题,您无法确定是否会立即在流上接收所有数据。