当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

[简单]代码片段_4

发表于: 2014-11-24   作者:53873039oycg   来源:转载   浏览:
摘要:         很早前随手写的,欢迎提出更好的写法。          import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInpu

        很早前随手写的,欢迎提出更好的写法。

        

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class IP_Connect_Mutil {
	public static void main(String[] args) throws Exception {
		IP_Connect_Mutil t = new IP_Connect_Mutil();
		t.getAllReachableIP(8);
	}

	//数据来源:[@http://cb.e-fly.org:81/archives/goagent-iplist.html]
	//https://github.com/justjavac/Google-IPs
	public void getAllReachableIP(int subSize) throws Exception {
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
				"f:/saveFile/temp/google_ip9.txt"), "utf-8");
		BufferedWriter bw = new BufferedWriter(osw);
		ExecutorService es = Executors.newFixedThreadPool(subSize);
		List<String> ipList = getTextIP("src/test.txt");
		List<Future<String>> resultList = new ArrayList<Future<String>>();
		int size = ipList.size();
		for (int i = 0; i < subSize; i++) {
			PingTask ping = null;
			if (i == subSize - 1) {
				ping = new PingTask(ipList, (subSize - 1) * size / subSize + 1,
						size);
			} else {
				ping = new PingTask(ipList, i * size / subSize
						+ (i > 0 ? 1 : 0), (i + 1) * size / subSize);
			}
			Future<String> future = es.submit(ping);
			resultList.add(future);
		}
		for (Future<String> future : resultList) {
			String result = future.get();
			bw.write(result);
			bw.write("\r\n");
			bw.flush();
		}
		bw.close();
		es.shutdown();
		try {
			es.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public List<String> getTextIP(String fileName) throws Exception {
		InputStreamReader ir = new InputStreamReader(new FileInputStream(
				fileName), "utf-8");
		BufferedReader br2 = new BufferedReader(ir);
		String str = null;
		Set<String> resultList = new HashSet<String>();
		while ((str = br2.readLine()) != null) {
			if (str.trim().length() == 0) {
				continue;
			}
			//以空格分割
			String[] strArr = str.split("\\s+");
			for (String sub : strArr) {
				if (sub.trim().length() > 0) {
					resultList.add(sub);
				}
			}
		}
		br2.close();
		List<String> list = new ArrayList<String>();
		list.addAll(resultList);
		return list;
	}

}

class PingTask implements Callable<String> {
	private List<String> ipList;
	private int startIndex;
	private int endIndex;

	public PingTask(List<String> ipList, int startIndex, int endIndex) {
		super();
		this.ipList = ipList;
		this.startIndex = startIndex;
		this.endIndex = endIndex;
		System.out.println(ipList.size() + "---s=" + startIndex + "----e="
				+ endIndex);
	}

	public String call() throws Exception {
		StringBuffer sb = new StringBuffer();
		for (int i = startIndex; i < endIndex; i++) {
			Socket socket = null;
			try {
				socket = new Socket();
				socket.connect(new InetSocketAddress(ipList.get(i), 80), 5000);
				sb.append(' ').append(ipList.get(i)).append("\r\n");
			} catch (Exception e) {
				System.err.println(String.format(
						"index=%s ip=%s  not reachable", i, ipList.get(i)));
			} finally {
				if (socket != null)
					try {
						socket.close();
					} catch (IOException e) {
					}
			}
		}
		return sb.toString();
	}
}

    全文完

 

[简单]代码片段_4

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
下面的代码很简单,替换参数功能很早前有人写过,而且写的还不错,直接贴代码。 import java.io.FileOu
许久前写的简单的tableView例子,主要针对处理缓存、协议、数据源datasource、局部刷新等问题进行解
工作中遇到需要合并相同用户的内容,数据如下: 代码如下: import java.text.ParseException; import
代码片段:代码片段可以使我们代码敲得更方便,效率更高,但是不推荐新人使用 下面详细说一下代码片
从Visual Studio 2005开始,IDE支持代码片段。代码片段以代码缩写和TAB自动切换输入点的方式,提高
实现IDisposable的代码片段 1 ~DemoType() 2 { 3 this.Dispose(); 4 } 5 6 #region IDisposable Mem
http://www.csdn.net/article/2013-07-23/2816316-10-php-snippets-for-developers 直接拿来用,10
如何在一个apk中调用另外一个apk中的activity? 系统提供了很多可以直接调用的Activity,通过指定的
摘自:http://blog.csdn.net/shakespeare001/article/details/7926783 Adapter是连接后端数据和前端
1、垂直对齐 如果你用CSS,则你会有困惑:我该怎么垂直对齐容器中的元素?现在,利用CSS3的Transfor
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号