import java.net.*;
import java.io.IOException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PortScanner {
public static void main(String[] args) {
InetAddress ia=null;
String host=null;
try {
host=JOptionPane.showInputDialog("Enter the Host name to scan:\n example: example.com");
if(host!=null){
ia = InetAddress.getByName(host);
scan(ia); }
}
catch (UnknownHostException e) {
System.err.println(e);
}
System.out.println("Bye from NFS");
//System.exit(0);
}
public static void scan(final InetAddress remote) {
//variables for menu bar
int port=0;
String hostname = remote.getHostName();
for (port = 70; port < 65536; port++) {
try {
Socket s = new Socket(remote,port);
System.out.println("Server is listening on port " + port+ " of " + hostname);
s.close();
break;
}
catch (IOException ex) {
// The remote host is not listening on this port
System.out.println("Server is not listening on port " + port+ " of " + hostname);
}
}//for ends
}
}
請幫幫我。我可以加快掃描端口過程嗎?
我的意思是這組代碼 爲(端口= 70;端口<65536;端口++){ 嘗試{ 插座S =新的Socket(遠程,端口); System.out.println(「服務器正在偵聽」+ hostname「的端口」+ port +「); s.close(); 休息; } 它可以更快處理?導致需要花費很多時間逐一掃描端口。 – Otip88
處理時間不會增加太多,因爲網絡連接握手性能幾乎不受您的控制。只是試圖平行打開套接字而不是等待每個套接字完成,你可能會有更好的性能。 –
好的...謝謝你的提示..歡呼隊友。 – Otip88