try{
}catch(Exception ex)
{
  如占用,一定报错
}

解决方案 »

  1.   

    for (int i = 0; i <255; i++){
        try {
          s = new ServerSocket(i);    
        }
        catch (IOException ex) {
          System.out.println("端口﹕" + i + " 已被占用");
        }
        try {
          s.close;
        }
        catch (IOException ex) {
          System.out.println("");
        }
    }
      

  2.   

    import java.net.*;
    import java.io.*;public class HighPortScanner {
    public static void main(String args[]) {   String host="localhost";
      int begin=0;
      int end=65336;
      if(args.length>0) {
             host=args[0];
      }
          if(args.length>1) {
      begin=Integer.parseInt(args[1]);
      end=Integer.parseInt(args[2]);
          }   try{
      InetAddress theAddress=InetAddress.getByName(host);   for(int i=begin;i<end;i++) {   BufferedReader networkIn=null;   try {
      Socket theSocket=new Socket(theAddress,i);
      System.out.println("there is a serve on port"+i+" of "+host);
      networkIn=new BufferedReader(new InputStreamReader(theSocket.getInputStream()));
       System.out.println(networkIn.readLine());
      }   catch (IOException e) {
           System.out.println("there is no service on port "+i);
      }   }//END FOR
      }//END TRY   catch(UnknownHostException e) {
            System.err.println(e);
      }
    }//END MAIN}
      

  3.   

    support it
    回复人: littlecpu(嘿,哥们,你欠我分,快给咯) ( ) 信誉:100  2003-05-15 12:35:00  得分:0 
     
     
      try{
    }catch(Exception ex)
    {
      如占用,一定报错
    }