- import java.io.IOException;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.net.UnknownHostException;
- public class sampletest
- {
- public static void main(String[] args)
- {
- for(int i=1;i<256;i++)
- {
- try
- {
- InetAddress localHost=InetAddress.getLocalHost();
- Socket socket=new Socket(localHost,i);
- System.out.println("本机已经使用了端口:"+i);
- }
- catch(UnknownHostException e)
- {
- //e.printStackTrace();
- }
- catch(IOException e)
- {
- // e.printStackTrace();
- }
- }
- System.out.println("执行完毕!");
- }
- }
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class sampletest
{
public static void main(String[] args)
{
for(int i=1;i<256;i++)
{
try
{
InetAddress localHost=InetAddress.getLocalHost();
Socket socket=new Socket(localHost,i);
System.out.println("本机已经使用了端口:"+i);
}
catch(UnknownHostException e)
{
//e.printStackTrace();
}
catch(IOException e)
{
// e.printStackTrace();
}
}
System.out.println("执行完毕!");
}
}
- import java.io.IOException;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.net.UnknownHostException;
- public class sampletest implements Runnable
- {
- public sampletest(){
- }
- public void searchPort(){
- System.out.println("本机已经使用了端口:");
- for(int i=1;i<256;i++)
- {
- try
- {
- InetAddress localHost=InetAddress.getLocalHost();
- Socket socket=new Socket(localHost,i);
- System.out.print(" "+i+":使用");
- }
- catch(UnknownHostException e)
- {
- //e.printStackTrace();
- }
- catch(IOException e)
- {
- System.out.print(" "+i+":空闲");
- // e.printStackTrace();
- }
- }
- System.out.println("执行完毕!");
- }
- public void run() {
- for(;;){
- try {
- Thread.sleep(3000);
- this.searchPort();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- // TODO Auto-generated method stub
- }
- public static void main(String[] args){
- Runnable rb = new sampletest();
- new Thread(rb).start();
- }
- }