我要做的是遍历搜索出局域网在线的IP,然后用Socket 看看那些80端口是打开的
现在遇到个问题是,遍历IP和用SOcekt 的时候耗时太多,大约需要23秒,哪位高手能帮我改进下,提高下效率,必重谢
import java.io.*;
import java.util.*;
import java.net.*;
import sss.LocalIp;public class Ip { static public Vector ping; //ping 后的结果集
static int threadCount = 0; //当前线程的数量, 防止过多线程摧毁电脑
public Ip() {
ping = new Vector();
}
public void Ping(String ip) throws Exception {
//最多30个线程
while (threadCount > 30) {
Thread.sleep(50);
}
threadCount += 1;
PingIp p = new PingIp(ip);
p.start(); } public void PingAll(String ip) throws Exception { for (int i = 1; i <= 255; i++) { //对所有局域网Ip
String iip = ip + i;
Ping(iip);
}
//等着所有Ping结束
while (threadCount > 0) {
Thread.sleep(50);
}
} public static void main(String[] args) throws Exception {
Ip ip = new Ip();
String strip = LocalIp.fucIp(); //通过LocalIp类得到本地IP前三段,192.168.195.
ip.PingAll(strip);
for (int i = 0; i < ip.ping.size(); i++) {
SingleSocket s = new SingleSocket(ip.ping.elementAt(i).toString());
//System.out.println(ip.ping.elementAt(i));
s.start();
}
} class PingIp extends Thread { public String ip; // IP
public PingIp(String ip) {
this.ip = ip;
}
public void run() {
try {
Process p = Runtime.getRuntime().exec("ping " + ip +
" -w 300 -n 1");
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
//读取结果行
for (int i = 1; i < 7; i++) {
input.readLine();
}
String line = input.readLine();
// System.out.println("OK!!"+ip+"--OUT:"+line); if ((line.length() < 17) ||
line.substring(8, 17).equals("timed out")) {}
//System.out.println(ip+":close");
else {
ping.addElement(ip);
}
//线程结束
threadCount -= 1;
} catch (IOException e) {}
}
}
}
class SingleSocket extends Thread {
private String ip;
public SingleSocket(String ip) {
this.ip = ip;
} public void run() {
try {
Socket s = new Socket(ip, 8080);
System.out.println(ip + ":" + s.toString()); } catch (Exception e) {
Thread.yield();
//System.out.println(ip + ":不存在的主机!");
}
}
}
现在遇到个问题是,遍历IP和用SOcekt 的时候耗时太多,大约需要23秒,哪位高手能帮我改进下,提高下效率,必重谢
import java.io.*;
import java.util.*;
import java.net.*;
import sss.LocalIp;public class Ip { static public Vector ping; //ping 后的结果集
static int threadCount = 0; //当前线程的数量, 防止过多线程摧毁电脑
public Ip() {
ping = new Vector();
}
public void Ping(String ip) throws Exception {
//最多30个线程
while (threadCount > 30) {
Thread.sleep(50);
}
threadCount += 1;
PingIp p = new PingIp(ip);
p.start(); } public void PingAll(String ip) throws Exception { for (int i = 1; i <= 255; i++) { //对所有局域网Ip
String iip = ip + i;
Ping(iip);
}
//等着所有Ping结束
while (threadCount > 0) {
Thread.sleep(50);
}
} public static void main(String[] args) throws Exception {
Ip ip = new Ip();
String strip = LocalIp.fucIp(); //通过LocalIp类得到本地IP前三段,192.168.195.
ip.PingAll(strip);
for (int i = 0; i < ip.ping.size(); i++) {
SingleSocket s = new SingleSocket(ip.ping.elementAt(i).toString());
//System.out.println(ip.ping.elementAt(i));
s.start();
}
} class PingIp extends Thread { public String ip; // IP
public PingIp(String ip) {
this.ip = ip;
}
public void run() {
try {
Process p = Runtime.getRuntime().exec("ping " + ip +
" -w 300 -n 1");
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
//读取结果行
for (int i = 1; i < 7; i++) {
input.readLine();
}
String line = input.readLine();
// System.out.println("OK!!"+ip+"--OUT:"+line); if ((line.length() < 17) ||
line.substring(8, 17).equals("timed out")) {}
//System.out.println(ip+":close");
else {
ping.addElement(ip);
}
//线程结束
threadCount -= 1;
} catch (IOException e) {}
}
}
}
class SingleSocket extends Thread {
private String ip;
public SingleSocket(String ip) {
this.ip = ip;
} public void run() {
try {
Socket s = new Socket(ip, 8080);
System.out.println(ip + ":" + s.toString()); } catch (Exception e) {
Thread.yield();
//System.out.println(ip + ":不存在的主机!");
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货