- 论坛徽章:
- 1
|
主要用于选择更合适的dns解析服务器。可以自定义配置需要测试的dns地址。
如果你们的宽带是电信联通或移动可以去查询它们所在城市的dns地址也可以加入测试。
dnstest.ini- #要进行解析的域名
- test www.baidu.com
- test www.oschina.com
- test tv.sohu.com
-
- #可用的dns。
- dns 8.8.8.8
- dns 8.8.4.4
- dns 114.114.114.114
- dns 114.114.115.115
- dns 223.5.5.5
- dns 223.6.6.6
- dns 1.2.4.8
- dns 210.2.4.8
- dns 208.67.222.222
- dns 208.67.220.220
- dns 101.226.4.6
- dns 123.125.82.6
- dns 123.125.83.6
复制代码 处理线程- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.SocketException;
- import java.net.UnknownHostException;
- import java.nio.ByteBuffer;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
-
- public class DNSSpeed implements Runnable {
- private final InetAddress dns;
- private final String dd;
-
- public DNSSpeed(String dns, String dd) throws UnknownHostException {
- this.dns = InetAddress.getByName(dns);
- this.dd = dd;
- }
-
- public void run() {
- byte[] data = toDnsBytes(dd);
- DatagramSocket dispatcher = null;
- DatagramPacket pack = null;
- boolean isSuccess = false;
- long start = System.currentTimeMillis();
- try {
- pack = new DatagramPacket(data, data.length, dns, 53);
- dispatcher = createDatagramSocket();
- dispatcher.setSoTimeout(1000);
- dispatcher.send(pack);
- data = new byte[512];
- pack = new DatagramPacket(data, data.length);
- dispatcher.receive(pack);
- if (pack.getLength() > 12) {//简单验证返回包
- isSuccess = true;
- }
- } catch (Exception e) {
- } finally {
- if (null != dispatcher) {
- dispatcher.close();
- }
- String info = "%-15s\t%-20s\t%s\t%4d\t";
- info = String.format(info, dns.getHostAddress(), dd, isSuccess, (System.currentTimeMillis() - start));
- System.out.println(info);
- }
- }
-
- /**
- * 组dns查询包
- * 2015年7月14日
- * @param dd
- * @return
- */
- protected static final byte[] toDnsBytes(String dd) {
- ByteBuffer b = ByteBuffer.allocateDirect(512);
- short id = (short) (3 + r.nextInt(1000));
- b.putShort(id);// id;
- b.put((byte) 0x01);
- b.put((byte) 0x00);
- b.putShort((short) (0x01 & 0xFFFF));
- b.putShort((short) (0x00 & 0xFFFF));
- b.putShort((short) (0x00 & 0xFFFF));
- b.putShort((short) (0x00 & 0xFFFF));
- String[] ss = dd.split("\\.");
- for (int i = 0; i < ss.length; i++) {
- byte[] bb = ss[i].getBytes();
- b.put((byte) (bb.length & 0xFF));
- b.put(bb);
- }
- b.put((byte) 0x00);
- b.putShort((short) 0x01);
- b.putShort((short) 0x01);
- b.flip();
- byte[] bb = new byte[b.remaining()];
- b.get(bb);
- return bb;
- }
-
- private static Random r = new Random();
-
- /**
- * 随机取一个 50000开始的端口
- * 2015年7月14日
- * @return
- */
- private static synchronized DatagramSocket createDatagramSocket() {
- DatagramSocket result = null;
- while (null == result) {
- try {
- int port = 50000 + r.nextInt(10000);
- result = new DatagramSocket(port);
- } catch (SocketException e) {
- result = null;
- } finally {
- }
- }
- return result;
- }
-
- public static void main(String[] args) throws UnknownHostException {
- List<String> config = loadFile("dnstest.ini");
- List<String> dns = getConfig(config, "dns");
- List<String> test = getConfig(config, "test");
- for (String s : dns) {
- for (String t : test) {
- new DNSSpeed(s, t).run();
- }
- }
- }
-
- /**
- * 取key. 配置以 key value格式
- * 2015年7月14日
- * @param config
- * @param key
- * @return
- */
- protected static final List<String> getConfig(List<String> config, String key) {
- List<String> result = new ArrayList<String>();
- for (String ss : config) {
- ss = ss.trim();
- if (ss.startsWith(key)) {
- result.add(ss.substring(key.length()).trim());
- }
- }
- return result;
- }
-
- /**
- * 加载配置文件转list
- * 2015年7月14日
- * @param path
- * @return
- */
- protected static final List<String> loadFile(String path) {
- List<String> result = new ArrayList<String>();
- BufferedReader in = null;
- try {
- in = new BufferedReader(new FileReader(path));
- String s = null;
- while (null != (s = in.readLine())) {
- s = s.trim();
- if (!s.startsWith("#") && !s.isEmpty()) {//过滤掉空行及#号开头的配置
- result.add(s);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (null != in) {
- try {
- in.close();
- } catch (IOException e) {
- }
- }
- }
- return result;
- }
- }
复制代码 结果数据,主要看最后一位是解析时间- 8.8.8.8 www.baidu.com true 12
- 8.8.8.8 www.oschina.com true 4
- 8.8.8.8 tv.sohu.com true 7
- 8.8.4.4 www.baidu.com true 7
- 8.8.4.4 www.oschina.com true 8
- 8.8.4.4 tv.sohu.com true 6
- 114.114.114.114 www.baidu.com true 5
- 114.114.114.114 www.oschina.com true 6
- 114.114.114.114 tv.sohu.com true 8
- 114.114.115.115 www.baidu.com true 5
- 114.114.115.115 www.oschina.com true 6
- 114.114.115.115 tv.sohu.com true 5
- 223.5.5.5 www.baidu.com true 41
- 223.5.5.5 www.oschina.com true 44
- 223.5.5.5 tv.sohu.com true 42
- 223.6.6.6 www.baidu.com true 40
- 223.6.6.6 www.oschina.com true 45
- 223.6.6.6 tv.sohu.com true 46
- 1.2.4.8 www.baidu.com true 8
- 1.2.4.8 www.oschina.com true 6
- 1.2.4.8 tv.sohu.com true 13
- 210.2.4.8 www.baidu.com true 6
- 210.2.4.8 www.oschina.com true 8
- 210.2.4.8 tv.sohu.com true 187
- 208.67.222.222 www.baidu.com true 5
- 208.67.222.222 www.oschina.com true 6
- 208.67.222.222 tv.sohu.com true 4
- 208.67.220.220 www.baidu.com true 5
- 208.67.220.220 www.oschina.com true 7
- 208.67.220.220 tv.sohu.com true 10
- 101.226.4.6 www.baidu.com true 5
- 101.226.4.6 www.oschina.com true 5
- 101.226.4.6 tv.sohu.com true 6
- 123.125.82.6 www.baidu.com true 5
- 123.125.82.6 www.oschina.com true 7
- 123.125.82.6 tv.sohu.com true 5
- 123.125.83.6 www.baidu.com true 5
- 123.125.83.6 www.oschina.com true 5
- 123.125.83.6 tv.sohu.com true 6
复制代码 |
|