iOS:如何减少FTP连接超时? [英] iOS: How to reduce an FTP connection timeout?

查看:161
本文介绍了iOS:如何减少FTP连接超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究使用远程存储数据的iPhone应用程序。该应用程序仅供我们的客户使用(无应用程序商店),我不允许指定应用程序的上下文。



我可以告诉你的唯一上下文是该应用程序允许输入代码,并下载与该代码相关的数据。



我们的客户告诉我们有时我们需要使用该应用程序地方互联网是不可访问或太慢。



已经绘制的解决方案如下:


  1. 我们将尝试将数据下载到本地FTP服务器上,该服务器实际上只是一个共享目录的路由器,其代码的名称可由已知IP访问(例如 ftp://192.168.1.1/some-path )。


  2. 如果失败,它会尝试从网络服务器上下载。


Nico Kreipke发现了一个名为FTPManager的超棒图书馆。 (点击此处查看它的GitHub



不幸的是,当FTP地址不可用时,大约需要一分钟超时并进入第二步。



任何人都可以帮助减少超时吗?也许快速ping到IP?如果是这样,怎么办?



感谢您的宝贵时间,


Tiago



更多信息



我尝试了一个灵感来自Rauru Ferro答案的解决方案。但不幸的是,它并没有帮助。
用于测试的代码如下:

 
NSString * ftp = [NSString stringWithFormat:@ftp:// user:password @%@ / sda1 /%@,ip,code];
NSURL * url = [NSURL URLWithString:ftp];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

我试过了几个超时值,但无济于事。它总是以超时错误结束。但是,下载代码可以从路由器的共享目录下载数据(使用相同的凭证)。 有一个连接到服务器时需要考虑的几件事情。首先要确定的是,我连接到服务器,第二个是服务器响应需要多长时间?。



使用FTP时,首先向服务器发送低级别的tcp / ip连接,然后服务器将以初始2xx级别的欢迎/ Hello消息进行响应。



使用高级别的类接口,如NSMutableURLRequest,可能很难区分连接超时和服务器响应超时。您可能连接到服务器,但服务器陷入困境,响应欢迎消息的速度很慢。



我的建议是,级别并使用通用BSD套接字对象,创建套接字然后连接到服务器。您还可以使用低级别的setsockopt()调用来限制connect()调用将等待连接的时间量。通过FTP,您还可以选择执行recv()以从服务器检索欢迎消息。您可以在此调用上设置不同的超时时间,以确保服务器及时响应。

稍高的级别,CFSocket类会为您提供更多封装来自BSD层,但你仍然应该能够突破连接时间和初始响应时间之间的差异。



希望有帮助!
Michael


I'm working on an iPhone app that uses data stored remotely. The app is to be used by our client only (no app store) and I'm not allow to specify the app's context.

The only context I can tell you is that the app allows a code to be entered, and downloads the data related to that code.

Our client told us that some times we'll need to use the app on places were internet isn't accessible or too slow.

The solution that has been drawn is the following:

  1. We'll try to download the data on a local FTP server, which is actually just a router sharing a directory with the code as its name, accessible by a known IP (e.g., ftp://192.168.1.1/some-path).

  2. If it fails, it tries to download from the web server.

I've found an awesome library called FTPManager by Nico Kreipke. (click here for its GitHub)

Unfortunately, when the FTP address isn't available, it take about one minute to timeout and move to the second step.

Does anyone can help out on reducing this timeout? Maybe making a quick ping to the IP? If so, how to do it?

Thanks for your time,
Tiago

Some More Info

I've tried a solution inspired by the answer of Rauru Ferro. But unfortunately it didn't help. The code used for testing follows:

NSString *ftp = [NSString stringWithFormat:@"ftp://user:password@%@/sda1/%@", ip, code];
NSURL *url = [NSURL URLWithString:ftp];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

I've tried several timeout values, but to no avail. It always ends with a timeout error. However, the downloading code is able to download data from the router's shared directory (using the same credentials).

解决方案

There are a few things to take into consideration when you connect to a server. The first thing to determine will be, "did I connect to the server" and the second will be "how long did it take for the server to respond?".

With FTP, the low level tcp/ip connection is first made to the server and then the server will respond with the initial 2xx level Welcome/Hello message.

When using a high level class interface such as NSMutableURLRequest, it might be difficult to distinguish between a connection timeout, and a server response timeout. It's possible that you are connecting to the server, but the server is bogged down and just very slow to respond with the Welcome message.

My recommendation would be to go low-level and use a generic BSD socket object, create the socket and then connect to the server. You can also use the low level setsockopt() call to limit the amount of time that the connect() call will wait for a connection. With FTP, you could also optionally do a recv() to retrieve the welcome message from the server. You could set a different timeout on this call to ensure that the server responds in a timely fashion.

At a slightly higher level, the CFSocket class will give you a bit more encapsulation from the BSD layer, but you should still be able to break out the difference between the connection time and the initial response time.

Hope that helps! Michael

这篇关于iOS:如何减少FTP连接超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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