gpt4 book ai didi

ios - NSStream 关​​闭得太快

转载 作者:行者123 更新时间:2023-11-29 03:28:18 24 4
gpt4 key购买 nike

我在 iOS 中进行客户端-服务器通信已经有一段时间了,但我遇到了一个我无法理解的问题。

我写了两个基本函数:一个向服务器发送数据,另一个从服务器接收数据。每个都有一个名为 timeout 的参数,它允许当前线程 sleep 并每 0.25 秒唤醒一次,直到达到超时:

-(ReturnCode) send : (NSData*)data :(int)timeOut
{
if(![self isConnectionOpened]) return KO;

float timer = 0;

while(![_outputStream hasSpaceAvailable])
{
[NSThread sleepForTimeInterval:0.25];
timer+=0.25;
if(timer == timeOut) break;
}

int ret = 0;
if([_outputStream hasSpaceAvailable]){
int lg = [data length];

ret = [_outputStream write:[data bytes] maxLength:lg];

if(ret == -1) return KO;
else return OK;
}

return TIMEOUT;
}

- (ReturnCode) receive : (NSData**)data : (int)timeOut : (int)maxLength
{
uint8_t buffer[maxLength];
int len;
NSMutableData* dataReceived = [[NSMutableData alloc] init];
if(! [self isConnectionOpened]) return KO;

float timer = 0;
while(![_inputStream hasBytesAvailable])
{
[NSThread sleepForTimeInterval:0.25];
timer+=0.25;
if(timer == timeOut) break;
}

if(![_inputStream hasBytesAvailable]) return TIMEOUT;

while ([_inputStream hasBytesAvailable]) {
len = [_inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
[dataReceived appendBytes:buffer length:len];
*data = dataReceived;
return OK;
}
}
return KO;

}

使用 iPhone 4 + iOS6,一切都很顺利。但在 iOS7 下,由于某些模糊原因,输入流和输出流过早关闭(引发 NSStreamEventErrorOccurred)。事实是,如果我在从服务器接收数据之前放置一个断点并使代码运行,它可以正常工作并且读取/写入流不会错误关闭。

所以我认为存在同步问题,但我不明白为什么...如果有人有想法,请帮助...

最佳答案

我找到了问题的根源。

实际上,要非常小心安排输入流和输出流的位置。事实上,我被告知 Apple 对象必须在主线程上执行,所以我这样安排它们:

dispatch_async(dispatch_get_main_queue(), ^ {
[_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

[_inputStream open];
[_outputStream open];
});

但实际上,最好将它们调度到当前线程的当前循环上,而不是在主线程上调度调度操作:

     [_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

[_inputStream open];
[_outputStream open];

关于ios - NSStream 关​​闭得太快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20165314/

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