一個簡單的利用線程池技術實現端口掃描(TCP)的小程序
關鍵代碼如下
// 掃描本機
private void getLocal()
{
String ip = getIP();
String portStart = txPortStart
String portEnd = txPortEnd
if (portStart
return;
int s =
int e =
try {
s = Integer
e = Integer
} catch (Exception ex) {
JOptionPane
return;
}
// 檢查端口是否超出范圍
if (! (checkPort(s) && checkPort(e)))
{
JOptionPane
return;
}
scann(ip
runThread(); // 啟動線程
}
private String getIP()
{
try {
InetAddress addr = InetAddress
return addr
}
catch (Exception e)
{
JOptionPane
}
return null;
}
// 掃描單個IP
private void scann(String ip
{
// 將所有按鈕設為不可用
setBtnEdit(false);
status
String[] add = {ip
table
exec = Executors
for (int i = startPort; i <= endPort; i++)
exec
exec
}
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27724.html