gpt4 book ai didi

ios - NSInputStream 没有收到所有数据

转载 作者:行者123 更新时间:2023-11-29 01:49:06 25 4
gpt4 key购买 nike

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

例如
服务器发送数据(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");
}
}

最佳答案

正如 @Manav 所说,您可以期望以多个 block 的形式接收流,只需将每个 block 附加到 NSMutableData 直到获得 NSStreamEventEndEncountered

关于ios - NSInputStream 没有收到所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31602357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com