How to find the IP address with the best connectivity from a resolved list

更新时间:
复制 MD 格式

Problem description

A domain name can have multiple IP addresses. HTTPDNS returns all active IP addresses, but does not sort them.

Solution

You can use the IP ranking API to sort the IP addresses and find the one with the best connectivity. The following code provides examples:

Android:
ArrayList<IPRankingBean> list = new ArrayList<IPRankingBean>();
list.add(new IPRankingBean("www.aliyun.com", 8080));
new InitConfig.Builder().setIPRankingList(list);
Note

This method was added in version 2.3.2.

iOS:
 HttpDnsService *httpdns = [[HttpDnsService alloc] initWithAccountID:XXXX];
 NSDictionary *IPRankingDatasource = @{
                                 @"www.abc.com" : @80,
                                 @"www.cba.com" : @443,
                                 @"www.bac.com" : @443
                                 };
 [httpdns setIPRankingDatasource:IPRankingDatasource];
Harmony:
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { httpdns } from '@aliyun/httpdns';

const ACCOUNT_ID = 'Replace this with the Account ID from the Alibaba Cloud HTTPDNS console'
        
export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    httpdns.configService(ACCOUNT_ID, {
      context: this.context,
      // ************* IP ranking configuration start *************
      hostConfigsForIpRanking: [{
        host: 'www.aliyun.com',
        port: 80
      }]
      // ************* IP ranking configuration end *************
    })
  }
  // Other code is omitted
}

More information

Domain name probing uses a socket connection. It connects to each IP address in the list and dynamically sorts them. This ensures that the first IP address in the sorted list has the best availability.

Applicable to

  • HTTPDNS