import java.net.Socket;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
String remote = "69.163.44.171";
int i = 0;
do {
try {
Socket s = new Socket(remote,i);
System.out.println("Server is listening on port " + i+ " of " + remote);
s.close();
} catch (IOException ex) {
System.out.println("Server is not listening on port " + i+ " of " + remote);
}
i++;
} while(i == 55000);
}
輸出:Java的端口掃描器
Server is not listening on port 0 of 69.163.44.171
BUILD SUCCESSFUL (total time: 0 seconds)
我使用while循環,因爲它速度更快,現在的問題是,爲什麼它只能掃描一個端口?
While循環更快嗎? – 2010-03-01 21:03:28
編譯器錯誤:在'while(i == 55000)'後面缺少';'並使用正常的'for'會使代碼更易讀... – 2010-03-01 21:44:36
(a)這個程序是網絡綁定的。如何循環與其表現完全無關。 (b)這是一個'do'循環而不是'while'循環。 (c)比什麼更快?爲什麼? – EJP 2010-03-02 00:31:23