我正试图在我的iphone应用程序中的ftp服务器上发送一个文件。
在wifi和gsm:edge网络中似乎一切正常,但在3g网络中会出现一个错误(不总是,但非常频繁):
错误域=nsposixerrordomain
代码=12“操作不能
完整的。无法分配内存
在出现错误的代码下面:
- (void)stream:(NSStream*)aStream handleEvent:(NSStreamEvent)eventCode {
switch( eventCode ) {
case NSStreamEventHasSpaceAvailable: {
if( _readDataOffset == _readDataLimit ) {
NSInteger readDataLen = [_readStream read:[_readData mutableBytes] maxLength:kReadDataLength];
NSLog(@"readDataLen is %d",readDataLen);
if( -1 == readDataLen ) {
_error = [[_readStream streamError] retain];
_keepRunning = NO;
} else if( 0 == readDataLen ) {
_keepRunning = NO;
} else {
_readDataOffset = 0;
_readDataLimit = readDataLen;
}
}
if( _readDataOffset != _readDataLimit ) {
NSOutputStream* writeStream = (NSOutputStream*)aStream;
uint8_t *buffer = (void *)[_readData bytes];
// vvvv and here the value of writtenDataLen is often -1 (but only on 3G network)
NSInteger writtenDataLen = [writeStream write:&buffer[_readDataOffset] maxLength:_readDataLimit - _readDataOffset];
if( writtenDataLen > 0 ) {
_readDataOffset += writtenDataLen;
_writtenDataLen += writtenDataLen;
[self ftpPutDidWriteInternal];
} else if( -1 == writtenDataLen ) {
_error = [[writeStream streamError] retain];
_keepRunning = NO;
}
}
} break;
case NSStreamEventErrorOccurred: {
_error = [aStream.streamError retain];
_keepRunning = NO;
} break;
}
}
什么是重要的,整个发送是在单独的线程中完成的,它有自己的NSAutoReleSeCo水池。
有人知道这个问题吗?有什么建议吗?我将不胜感激。
更新:
我刚刚检查了流行的iPhone应用程序“FTP在旅途中”得到了相同的(?)在3G网络中发送文件时出现问题!没有错误处理,但传输停止。
更新2:
我不敢相信,但这是真的:苹果的SimpleFTPSample也受到了这个问题的影响。
最佳答案
这就是解决方案(或者说是解决方法):
您应该将writestream的属性设置为false,以关闭默认的持久连接
CFWriteStreamSetProperty( (CFWriteStreamRef)writeStreamRef, kCFStreamPropertyFTPAttemptPersistentConnection, kCFBooleanFalse ) ;