浏览 1751 次
锁定老帖子 主题:寻找可访问的随机网站(代码)
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-03-19
package corejava.chapter.network; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Calendar; import java.util.Random; import java.util.Vector; /** * 随机搜寻可以访问的网址 */ public class LookforWWWSite { private static Random r = new Random(); private static final int WANTSITESCOUNT = 10; /** * 随机生成指定长度字符的2、3级域名 * * @param bits * @return */ private static String getPrename (int bits) { char[] chars = new char[bits]; for (int i = 0; i < bits; i++) { chars[i] = (char) (r.nextInt(26) + 97); //a~z } return "www." + new String(chars); } /** * 网址生成规则,3级别域名+ 2级域名 + 1级域名 * 3级域名:www 2级域名:3-5位字符组合 * * @param args */ public static void main(String[] args) { Vector<String> sites = new Vector<String>(); String[] name1 = { ".com", ".net", ".org", ".cn" }; System.out.println("Start at "+ Calendar.getInstance().getTime()); long start = Calendar.getInstance().getTimeInMillis(); while (true) { String prename = getPrename(3 + r.nextInt(3)); // 3~5 for (String n1 : name1) { try { String site = prename + n1; InetAddress.getAllByName(site); if (!sites.contains(site)) { sites.add(site); if (sites.size() >= WANTSITESCOUNT) break; } } catch (UnknownHostException e) { } } if (sites.size() >= WANTSITESCOUNT) break; } long end = Calendar.getInstance().getTimeInMillis(); System.out.println("Take time in millis : "+ (end - start)); for (String site : sites) System.out.println(site); } } 输出结果: Start at Wed Mar 19 14:56:18 CST 2008 Take time in millis : 41672 www.nag.com www.nag.net www.nag.org www.rdlz.com www.rdlz.cn www.ctm.com www.ctm.net www.ctm.org www.ctm.cn www.pqqz.com 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |