简体   繁体   English

如何确定哪个服务器与Android中的用户更近/更快?

[英]How to determine which server is closer/faster to the user in Android?

On the first app start I download ~50 mb of files and then unzip them.. 在第一个应用程序启动时,我下载了约50 mb的文件,然后将其解压缩。
I have users from around the world and I have 3 different server that I can host the files, so I thought of using them all and check by code which server is the fastest/closest to user and to use that.. 我有来自世界各地的用户,并且我有3个可以托管文件的服务器,因此我想全部使用它们并通过代码检查哪个服务器对用户最快/最接近,并使用它。

How can I achieve that? 我该如何实现?

Why don't you use the Google Play service for adding "expansion files"? 为什么不使用Google Play服务添加“扩展文件”? See here: http://developer.android.com/guide/google/play/expansion-files.html 参见此处: http : //developer.android.com/guide/google/play/expansion-files.html

I didn't try it personally, but it should definitely help you. 我没有亲自尝试过,但是绝对可以帮助您。

Maybe send a simple request to a kind of ping.php script to each server, measure the time taken to read the response and pick the best one. 也许向每个服务器发送一种简单的请求到一种ping.php脚本,测量读取响应所花费的时间并选择最佳响应。 Don't forget to send a first request to a dummy server or to download a web page in order for the phone to activate the network and set it to best performance possible (else your first server might always look slow compared to the next ones). 别忘了向虚拟服务器发送第一个请求或下载网页,以便电话激活网络并将其设置为可能的最佳性能(否则,与下一个服务器相比,第一个服务器可能看起来总是很慢) 。

I've never done this before, so this is just a guess, but if you want to find out which is closer: 我以前从未做过,所以这只是一个猜测,但是如果您想找出更接近的那个:

Get the User's IP Address: How to get IP address of the device from code? 获取用户的IP地址: 如何从代码中获取设备的IP地址?

Do a reverse DNS lookup on the IP address, which will give you something like this: c-24-131-5-42.hsd1.va.comcast.net 在IP地址上进行反向DNS查找,这将为您提供以下信息:c-24-131-5-42.hsd1.va.comcast.net

By looking at the resulting string, you can tell that I live in VA. 通过查看结果字符串,可以知道我住在弗吉尼亚州。 Using the country/state figure out which is closer to your server and use it. 使用国家(地区)/州来找出最接近您的服务器并使用它。

To do this yourself; 自己做; you'll need to query a geo ip service with the IP address of the device to get its approximate location; 您需要使用设备的IP地址查询geo ip服务以获取其大概位置; then compare that with which server to best serve the file from. 然后将其与最好从哪个服务器提供文件的服务器进行比较。 This has its own problems (proxy/vpn tunnels being one of them). 这有其自身的问题(代理/ vpn隧道就是其中之一)。

Second is to ping your server. 第二是对您的服务器执行ping操作。 The problem here could be that ping may be blocked by the provider (especially if the device is on not on wifi); 这里的问题可能是提供商可能阻止了ping操作(特别是如果设备不在wifi上)。 even if they are on wifi, ping may be blocked for external addresses and only allowed within the network. 即使它们在wifi上,也可能会阻止ping外部地址,并且只能在网络内允许。 In this case; 在这种情况下; the ping will always fail. ping总是会失败。

So the reliable way is to use a Content Delivery Network . 因此,可靠的方法是使用内容交付网络 They will take care of serving the file from the closest possible server automatically. 他们将负责自动从最近的服务器提供文件。 You don't have to modify your application or manage different servers. 您无需修改​​应用程序或管理其他服务器。

You could also try to establish a TCP connection with every server that you have and track the time taken to do that. 您还可以尝试与您拥有的每台服务器建立TCP连接,并跟踪花费的时间。 Then use the one that has the minimum time. 然后使用最短的时间。

Or have the device pull a file from each server containing the server's known availability. 或者让设备从每个服务器中提取一个包含服务器已知可用性的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM