如下實例演示瞭如何檢測端口是否已經使用: 實例 import java . net .*;java
import java . io .*;markdown
public class Main {socket
public static void main ( String [ ] args ) { Socket Skt ; String host = " localhost " ; if ( args . length > 0 ) { host = args [ 0 ] ; } for ( int i = 0 ; i < 1024 ; i ++ ) { try { System . out . println ( " 查看 " + i ) ; Skt = new Socket ( host , i ) ; System . out . println ( " 端口 " + i + " 已被使用 " ) ; } catch ( UnknownHostException e ) { System . out . println ( " Exception occured " + e ) ; break ; } catch ( IOException e ) { } } }
}
以上代碼運行輸出結果爲:ide
……atom
查看 17code
查看 18get
查看 19string
查看 20it
查看 21io
端口 21 已被使用
查看 22
查看 23
查看 24
……
也能夠指定主機的端口: Main.java 文件 import java . io . IOException ;
import java . net . InetSocketAddress ;
import java . net . Socket ;
import java . net . SocketAddress ;
import java . net . SocketTimeoutException ;
public class Main { public static void main ( String [ ] args ) { // 檢測本地 80 端口 log ( isSocketAliveUitlitybyCrunchify ( " localhost " , 80 ) ) ; // 檢測本地 8080 端口 log ( isSocketAliveUitlitybyCrunchify ( " localhost " , 8080 ) ) ; // 檢測本地 8081 端口 log ( isSocketAliveUitlitybyCrunchify ( " localhost " , 8081 ) ) ; // 檢測 runoob.com 的 80 端口 log ( isSocketAliveUitlitybyCrunchify ( " runoob.com " , 80 ) ) ; // 檢測 runoob.com 的 443 端口 log ( isSocketAliveUitlitybyCrunchify ( " runoob.com " , 443 ) ) ; // 檢測 runoob.com 的 81 端口 log ( isSocketAliveUitlitybyCrunchify ( " runoob.com " , 81 ) ) ; } /* * * 判斷主機端口 * * @param hostName * @param port * @return boolean - true/false */ public static boolean isSocketAliveUitlitybyCrunchify ( String hostName , int port ) { boolean isAlive = false ; // 建立一個套接字 SocketAddress socketAddress = new InetSocketAddress ( hostName , port ) ; Socket socket = new Socket ( ) ; // 超時設置,單位毫秒 int timeout = 2000 ; log ( " hostName: " + hostName + " , port: " + port ) ; try { socket . connect ( socketAddress , timeout ) ; socket . close ( ) ; isAlive = true ; } catch ( SocketTimeoutException exception ) { System . out . println ( " SocketTimeoutException " + hostName + " : " + port + " . " + exception . getMessage ( ) ) ; } catch ( IOException exception ) { System . out . println ( " IOException - Unable to connect to " + hostName + " : " + port + " . " + exception . getMessage ( ) ) ; } return isAlive ; } private static void log ( String string ) { System . out . println ( string ) ; } private static void log ( boolean isAlive ) { System . out . println ( " 是否真正在使用: " + isAlive + "
" ) ;
}
}
以上代碼運行輸出結果爲:
hostName: localhost, port: 80
IOException - Unable to connect to localhost:80. Connection refused
是否真正在使用: true
hostName: localhost, port: 8080
IOException - Unable to connect to localhost:8080. Connection refused
是否真正在使用: false
hostName: localhost, port: 8081
IOException - Unable to connect to localhost:8081. Connection refused
是否真正在使用: false
hostName: runoob.com, port: 80
是否真正在使用: true
hostName: runoob.com, port: 443
是否真正在使用: true
hostName: runoob.com, port: 81
SocketTimeoutException runoob.com:81. connect timed out
是否真正在使用: false