我正在用ftp協議下載文件。現在,爲了檢查處理錯誤的能力,我模擬了一些網絡錯誤的發生。處理網絡InputStream中的代碼如下:如何處理ftp下載請求塊
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
// An NSStream delegate callback that's called when events happen on our
// network stream.
{
#pragma unused(aStream)
assert(aStream == self.networkStream);
switch (eventCode) {
case NSStreamEventOpenCompleted: {
self.connected = YES;
} break;
case NSStreamEventHasBytesAvailable: {
NSInteger bytesRead;
uint8_t buffer[32768];
// Pull some data off the network.
bytesRead = [self.networkStream read:buffer maxLength:sizeof(buffer)];
DLog(@"%@,byteRead:%d",self.urlInput,bytesRead);
if (bytesRead == -1) {
[self _stopReceiveWithStatus:@"Network read error"];
} else if (bytesRead == 0) {
[self _stopReceiveWithStatus:@"success"];
} else {
NSInteger bytesWritten;
NSInteger bytesWrittenSoFar;
bytesWrittenSoFar = 0;
do {
bytesWritten = [self.fileStream write:&buffer[bytesWrittenSoFar] maxLength:bytesRead - bytesWrittenSoFar];
DLog(@"%@,bytesWritten:%d",self.urlInput,bytesWritten);
assert(bytesWritten != 0);
if (bytesWritten == -1) {
[self _stopReceiveWithStatus:@"File write error"];
break;
} else {
bytesWrittenSoFar += bytesWritten;
}
} while (bytesWrittenSoFar != bytesRead);
}
} break;
case NSStreamEventHasSpaceAvailable: {
assert(NO); // should never happen for the output stream
} break;
case NSStreamEventErrorOccurred: {
[self _stopReceiveWithStatus:@"Stream open error"];
} break;
case NSStreamEventEndEncountered: {
assert(NO);
} break;
default: {
assert(NO);
} break;
}
}
如果我手動關閉無線網絡連接或關閉我的無線路由器(網絡連接標誌是關閉的),一個「NSStreamEventErrorOccurred」將返回和下載過程將被正確終止。但是,如果我關閉調制解調器,同時保持無線路由器打開(網絡連接標誌打開)。下載過程停留在「NSStreamEventHasBytesAvailable」情況。即使打開互聯網連接後,它仍然卡住。
我想知道它爲什麼卡住了,我該如何檢測這種錯誤。我該如何處理這種情況?
我只是使用來自App Develop Library的使用NSStream的SimpleFTP。 NSURLConnection比NSStream更好嗎? – itenyh 2012-07-06 03:14:35
@itenyh「更好」取決於你正在嘗試做什麼。一般規則是儘可能使用最高級別的API,並且NSURLConnection比NSStream高得多。我不明白爲什麼FTP需要NSStream。 – 2012-07-06 16:36:03