扫描计算机端口是否被占用

I

public class CheckPortI {
    public static void main(String[] args) {
        for (int i = 0, port = 0; i < 65535; i++) {
            try {
                ServerSocket ss = new ServerSocket(port);
            } catch (Exception e) {
                System.out.println("port " + port + " 已经被占用");
            }
        }
    }
}

II
public class ScanUsedSockets {

private static int counter;

private static synchronized int next() {
counter
+= 1;
if (counter > 65535) {
counter
= -1;
}
else if (counter % 1000 == 999) {
System.out.println(
"..." + counter);
}
return counter;
}

public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread() {
@Override
public void run() {
int port = ScanUsedSockets.next();
while (port > -1) {
try {
new ServerSocket(port).close();
Thread.sleep(
100);
}
catch (IOException e) {
System.out.println(
"端口 " + port + " 已经被占用。");
}
catch (InterruptedException e) {
e.printStackTrace();
}
port
= ScanUsedSockets.next();
}
}
}.start();
}
}

}


III

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TcpPortScanner {
 private static final int MAX_THREAD_NUM = 700;
 private static final int MAX_TIMEOUT = 3000;
 private static final String HOSTNAME = "127.0.0.1";
 @SuppressWarnings("unchecked")
 public static void main(String[] args) {
  final class Worker implements Callable<Boolean> {
   private int port = 0;
   public Worker(int port) {
    if (port < 0 || port > 0xFFFF) {
     throw new IllegalArgumentException("port out of range:"
       + port);
    }
    this.port = port;
   }
   public Boolean call() throws Exception {
    boolean open = false;
    Socket tcpskt = null;
    try {
     tcpskt = new Socket();
     tcpskt.setSoLinger(false, 0);
     tcpskt.setKeepAlive(false);
     tcpskt.setTcpNoDelay(true);
     tcpskt.connect(new InetSocketAddress(HOSTNAME, this.port),
       MAX_TIMEOUT);
     open = tcpskt.isConnected();
    } catch (SocketTimeoutException e) {
    } catch (SocketException e) {
    } catch (IOException e) {
    } finally {
     if (tcpskt != null && !tcpskt.isClosed()) {
      try {
       tcpskt.close();
      } catch (IOException e) {
      }
     }
    }
    return Boolean.valueOf(open);
   }
  }
  Future<Boolean>[] result = new Future[65536];
  ExecutorService tPool = Executors.newFixedThreadPool(MAX_THREAD_NUM);
  for (int i = 1; i < result.length; i++) {
   result[i] = tPool.submit(new Worker(i));
  }
  tPool.shutdown();
  try {
   System.out.println("TCP Port");
   System.out.println("=====================");
   for (int i = 1; i < result.length; i++) {
    if (result[i].get().booleanValue()) {
     System.out.println(i);
    }
   }
   System.out.println("完成 TCP 端口扫描!");
  } catch (InterruptedException e) {
  } catch (ExecutionException e) {
  }
 }
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值