亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1451 | 回復: 0
打印 上一主題 下一主題

模擬dns優(yōu)選,處理dns的響應時間 [復制鏈接]

論壇徽章:
1
數(shù)據(jù)庫技術版塊每日發(fā)帖之星
日期:2015-07-11 22:20:00
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2015-07-14 09:52 |只看該作者 |倒序瀏覽
主要用于選擇更合適的dns解析服務器?梢宰远x配置需要測試的dns地址。
如果你們的寬帶是電信聯(lián)通或移動可以去查詢它們所在城市的dns地址也可以加入測試。
dnstest.ini
  1. #要進行解析的域名
  2. test www.baidu.com
  3. test www.oschina.com
  4. test tv.sohu.com

  5. #可用的dns。
  6. dns 8.8.8.8
  7. dns 8.8.4.4
  8. dns 114.114.114.114
  9. dns 114.114.115.115
  10. dns 223.5.5.5
  11. dns 223.6.6.6
  12. dns 1.2.4.8
  13. dns 210.2.4.8
  14. dns 208.67.222.222
  15. dns 208.67.220.220
  16. dns 101.226.4.6
  17. dns 123.125.82.6
  18. dns 123.125.83.6
復制代碼
處理線程
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.net.DatagramPacket;
  5. import java.net.DatagramSocket;
  6. import java.net.InetAddress;
  7. import java.net.SocketException;
  8. import java.net.UnknownHostException;
  9. import java.nio.ByteBuffer;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Random;

  13. public class DNSSpeed implements Runnable {
  14.     private final InetAddress dns;
  15.     private final String dd;

  16.     public DNSSpeed(String dns, String dd) throws UnknownHostException {
  17.         this.dns = InetAddress.getByName(dns);
  18.         this.dd = dd;
  19.     }

  20.     public void run() {
  21.         byte[] data = toDnsBytes(dd);
  22.         DatagramSocket dispatcher = null;
  23.         DatagramPacket pack = null;
  24.         boolean isSuccess = false;
  25.         long start = System.currentTimeMillis();
  26.         try {
  27.             pack = new DatagramPacket(data, data.length, dns, 53);
  28.             dispatcher = createDatagramSocket();
  29.             dispatcher.setSoTimeout(1000);
  30.             dispatcher.send(pack);
  31.             data = new byte[512];
  32.             pack = new DatagramPacket(data, data.length);
  33.             dispatcher.receive(pack);
  34.             if (pack.getLength() > 12) {//簡單驗證返回包
  35.                 isSuccess = true;
  36.             }
  37.         } catch (Exception e) {
  38.         } finally {
  39.             if (null != dispatcher) {
  40.                 dispatcher.close();
  41.             }
  42.             String info = "%-15s\t%-20s\t%s\t%4d\t";
  43.             info = String.format(info, dns.getHostAddress(), dd, isSuccess, (System.currentTimeMillis() - start));
  44.             System.out.println(info);
  45.         }
  46.     }

  47.     /**
  48.      * 組dns查詢包
  49.      * 2015年7月14日
  50.      * @param dd
  51.      * @return
  52.      */
  53.     protected static final byte[] toDnsBytes(String dd) {
  54.         ByteBuffer b = ByteBuffer.allocateDirect(512);
  55.         short id = (short) (3 + r.nextInt(1000));
  56.         b.putShort(id);// id;
  57.         b.put((byte) 0x01);
  58.         b.put((byte) 0x00);
  59.         b.putShort((short) (0x01 & 0xFFFF));
  60.         b.putShort((short) (0x00 & 0xFFFF));
  61.         b.putShort((short) (0x00 & 0xFFFF));
  62.         b.putShort((short) (0x00 & 0xFFFF));
  63.         String[] ss = dd.split("\\.");
  64.         for (int i = 0; i < ss.length; i++) {
  65.             byte[] bb = ss[i].getBytes();
  66.             b.put((byte) (bb.length & 0xFF));
  67.             b.put(bb);
  68.         }
  69.         b.put((byte) 0x00);
  70.         b.putShort((short) 0x01);
  71.         b.putShort((short) 0x01);
  72.         b.flip();
  73.         byte[] bb = new byte[b.remaining()];
  74.         b.get(bb);
  75.         return bb;
  76.     }

  77.     private static Random r = new Random();

  78.     /**
  79.      * 隨機取一個 50000開始的端口
  80.      * 2015年7月14日
  81.      * @return
  82.      */
  83.     private static synchronized DatagramSocket createDatagramSocket() {
  84.         DatagramSocket result = null;
  85.         while (null == result) {
  86.             try {
  87.                 int port = 50000 + r.nextInt(10000);
  88.                 result = new DatagramSocket(port);
  89.             } catch (SocketException e) {
  90.                 result = null;
  91.             } finally {
  92.             }
  93.         }
  94.         return result;
  95.     }

  96.     public static void main(String[] args) throws UnknownHostException {
  97.         List<String> config = loadFile("dnstest.ini");
  98.         List<String> dns = getConfig(config, "dns");
  99.         List<String> test = getConfig(config, "test");
  100.         for (String s : dns) {
  101.             for (String t : test) {
  102.                 new DNSSpeed(s, t).run();
  103.             }
  104.         }
  105.     }

  106.     /**
  107.      * 取key. 配置以 key  value格式
  108.      * 2015年7月14日
  109.      * @param config
  110.      * @param key
  111.      * @return
  112.      */
  113.     protected static final List<String> getConfig(List<String> config, String key) {
  114.         List<String> result = new ArrayList<String>();
  115.         for (String ss : config) {
  116.             ss = ss.trim();
  117.             if (ss.startsWith(key)) {
  118.                 result.add(ss.substring(key.length()).trim());
  119.             }
  120.         }
  121.         return result;
  122.     }

  123.     /**
  124.      * 加載配置文件轉list
  125.      * 2015年7月14日
  126.      * @param path
  127.      * @return
  128.      */
  129.     protected static final List<String> loadFile(String path) {
  130.         List<String> result = new ArrayList<String>();
  131.         BufferedReader in = null;
  132.         try {
  133.             in = new BufferedReader(new FileReader(path));
  134.             String s = null;
  135.             while (null != (s = in.readLine())) {
  136.                 s = s.trim();
  137.                 if (!s.startsWith("#") && !s.isEmpty()) {//過濾掉空行及#號開頭的配置
  138.                     result.add(s);
  139.                 }
  140.             }
  141.         } catch (Exception e) {
  142.             e.printStackTrace();
  143.         } finally {
  144.             if (null != in) {
  145.                 try {
  146.                     in.close();
  147.                 } catch (IOException e) {
  148.                 }
  149.             }
  150.         }
  151.         return result;
  152.     }
  153. }
復制代碼
結果數(shù)據(jù),主要看最后一位是解析時間
  1. 8.8.8.8         www.baidu.com           true      12   
  2. 8.8.8.8         www.oschina.com         true       4   
  3. 8.8.8.8         tv.sohu.com             true       7   
  4. 8.8.4.4         www.baidu.com           true       7   
  5. 8.8.4.4         www.oschina.com         true       8   
  6. 8.8.4.4         tv.sohu.com             true       6   
  7. 114.114.114.114 www.baidu.com           true       5   
  8. 114.114.114.114 www.oschina.com         true       6   
  9. 114.114.114.114 tv.sohu.com             true       8   
  10. 114.114.115.115 www.baidu.com           true       5   
  11. 114.114.115.115 www.oschina.com         true       6   
  12. 114.114.115.115 tv.sohu.com             true       5   
  13. 223.5.5.5       www.baidu.com           true      41   
  14. 223.5.5.5       www.oschina.com         true      44   
  15. 223.5.5.5       tv.sohu.com             true      42   
  16. 223.6.6.6       www.baidu.com           true      40   
  17. 223.6.6.6       www.oschina.com         true      45   
  18. 223.6.6.6       tv.sohu.com             true      46   
  19. 1.2.4.8         www.baidu.com           true       8   
  20. 1.2.4.8         www.oschina.com         true       6   
  21. 1.2.4.8         tv.sohu.com             true      13   
  22. 210.2.4.8       www.baidu.com           true       6   
  23. 210.2.4.8       www.oschina.com         true       8   
  24. 210.2.4.8       tv.sohu.com             true     187   
  25. 208.67.222.222  www.baidu.com           true       5   
  26. 208.67.222.222  www.oschina.com         true       6   
  27. 208.67.222.222  tv.sohu.com             true       4   
  28. 208.67.220.220  www.baidu.com           true       5   
  29. 208.67.220.220  www.oschina.com         true       7   
  30. 208.67.220.220  tv.sohu.com             true      10   
  31. 101.226.4.6     www.baidu.com           true       5   
  32. 101.226.4.6     www.oschina.com         true       5   
  33. 101.226.4.6     tv.sohu.com             true       6   
  34. 123.125.82.6    www.baidu.com           true       5   
  35. 123.125.82.6    www.oschina.com         true       7   
  36. 123.125.82.6    tv.sohu.com             true       5   
  37. 123.125.83.6    www.baidu.com           true       5   
  38. 123.125.83.6    www.oschina.com         true       5   
  39. 123.125.83.6    tv.sohu.com             true       6
復制代碼
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP