当NSConnection关闭时,不断开与FTP的连接 [英] Not disconnecting from FTP when NSConnection closing

查看:346
本文介绍了当NSConnection关闭时,不断开与FTP的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Apple的 SimpleFTPSample 样本代码,特别是PUT部分。它有这种方法:

   - (void)stopSendWithStatus:(NSString *)statusString 
{
if(self.networkStream!= nil){
[self.networkStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
self.networkStream.delegate = nil;
[self.networkStream close];
self.networkStream = nil;
}
if(self.fileStream!= nil){
[self.fileStream close];
self.fileStream = nil;
}
[self sendDidStopWithStatus:statusString];
uploaderbusy = 0;
}

问题是,上传后应用程序没有关闭连接。执行上面的代码。



此代码:

  NSLog(@%lu,[self.networkStream streamStatus]); 
[self.networkStream close];
NSLog(@%lu,[self.networkStream streamStatus]);

提供此输出:

  2013-03-03 17:50:26.460 Stockuploader [575:303] 2 
2013-03-03 17:50:26.498 Stockuploader [575:303] 6

但连接保持打开并最终超时。这是FTP服务器的日志:

 (000006)03/03/2013 17.50.27  -  root2(192.168.0.3 )> 150接受连接
(000006)03/03/2013 17.50.27-root2(192.168.0.3)> 226传输OK
(000006)03/03/2013 17.52.28 - root2(192.168.0.3)> 421连接超时。
(000006)03/03/2013 17.52.28 - root2(192.168.0.3)>断开。

为什么?



如果我关闭程序,则连接立即在FTP上关闭。可能是因为我必须释放self.filestream?如果是的话,我如何使用ARC启用它?



---- UPDATE ---- b

我发现在我关闭连接之前,我必须发送QUIT到ftp服务器,以便让他知道我想断开连接。但如何实现发送QUIT命令到 SimpleFTPSample

解决方案

之前您打开连接,您必须将 kCFStreamPropertyFTPAttemptPersistentConnection 属性设置为FALSE:

  [self.networkStream setProperty:(id)kCFBooleanFalse 
forKey:(id)kCFStreamPropertyFTPAttemptPersistentConnection];

,则FTP流关闭时会关闭控制和数据连接。



从文档:


kCFStreamPropertyFTPAttemptPersistentConnection

FTP Attempt Persistent
设置和复制操作的连接流属性键。将此
属性设置为 kCFBooleanTrue ,以便重新使用现有服务器
连接;将此属性设置为 kCFBooleanFalse 以不重用
现有服务器连接。默认情况下,此属性设置为
kCFBooleanTrue



I am using an edited version of Apple's SimpleFTPSample sample code, specifically the "PUT" part. In it there is this method:

- (void)stopSendWithStatus:(NSString *)statusString
{
if (self.networkStream != nil) {
    [self.networkStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    self.networkStream.delegate = nil;
    [self.networkStream close];
    self.networkStream = nil;
}
if (self.fileStream != nil) {
    [self.fileStream close];
    self.fileStream = nil;
}
[self sendDidStopWithStatus:statusString];
uploaderbusy = 0;
}

Problem is that after the upload the application is NOT closing the connection. The code above is executed. Both IF statements are true and the code inside is executed.

This code:

    NSLog(@"%lu",[self.networkStream streamStatus]);
    [self.networkStream close];
    NSLog(@"%lu",[self.networkStream streamStatus]);

gives this output:

2013-03-03 17:50:26.460 Stockuploader[575:303] 2
2013-03-03 17:50:26.498 Stockuploader[575:303] 6

but the connection stays open and eventually times out. This is the log of the FTP server:

(000006)03/03/2013 17.50.27 - root2 (192.168.0.3)> 150 Connection accepted
(000006)03/03/2013 17.50.27 - root2 (192.168.0.3)> 226 Transfer OK
(000006)03/03/2013 17.52.28 - root2 (192.168.0.3)> 421 Connection timed out.
(000006)03/03/2013 17.52.28 - root2 (192.168.0.3)> disconnected.

Why is that?

if i CLOSE the program then the connection is instantly closed on the FTP. could it be that i have to deallocate the self.filestream? if so, how can i do it with ARC enabled?

----UPDATE----

I found that before i close the connection i have to send "QUIT" to the ftp server in order to let him know that i want to disconnect. but how can i implement the sending of "QUIT" command into the SimpleFTPSample?

解决方案

Before you open the connection, you have to set the kCFStreamPropertyFTPAttemptPersistentConnection property to FALSE:

[self.networkStream setProperty:(id)kCFBooleanFalse
                         forKey:(id)kCFStreamPropertyFTPAttemptPersistentConnection];

then the FTP stream closes both control and data connection when it is closed.

From the documentation:

kCFStreamPropertyFTPAttemptPersistentConnection
FTP Attempt Persistent Connection stream property key for set and copy operations. Set this property to kCFBooleanTrue to enable the reuse of existing server connections; set this property to kCFBooleanFalse to not reuse existing server connections. By default, this property is set to kCFBooleanTrue.

这篇关于当NSConnection关闭时,不断开与FTP的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆