我可以加快扫描端口进程吗?

时间:2009-11-27 05:40:53

标签: java performance networking

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
 }
}

请帮帮我。

2 个答案:

答案 0 :(得分:3)

我不确定这是否会加快速度,但由于每个套接字都与下一个套接字无关,您是否尝试制作更多线程,以便在旧套接字等待握手完成时创建新套接字。

答案 1 :(得分:0)

而不是使用行

Socket s = new Socket(remote,port);

你应该使用

Socket s = new Socket();
int timeout = 100; // milliseconds
s.connect( new InetSocketAddress( remote, port ), timeout );

通过这种方式,您无需等待默认TCP超时即可看到此端口被防火墙阻止而没有任何响应。