`
53873039oycg
  • 浏览: 824198 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

[简单]代码片段_4

    博客分类:
  • java
 
阅读更多

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

        

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();
	}
}

    全文完

 

0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics