回答
如果您不太關心性能,您可以嘗試使用ServerSocket類嘗試收聽端口。如果它拋出一個異常,它正被使用。
boolean portTaken = false;
ServerSocket socket = null;
try {
socket = new ServerSocket(_port);
} catch (IOException e) {
portTaken = true;
} finally {
if (socket != null)
try {
socket.close();
} catch (IOException e) { /* e.printStackTrace(); */ }
}
編輯:如果你試圖做的是選擇一個空閒的端口,然後new SocketServer(0)
會找到一個適合你。
最後使用清理(嘗試{...}最後{{}} {{ socket.close();} – 2009-01-12 07:51:34
你也可以設置「portTaken =(socket == null);」而不是在catch中做這個 – 2009-01-12 07:52:29
這試圖綁定到socket上的所有0.0.0.0)interfaces。您可以使用顯式地址來檢查特定地址上的特定端口。請參閱http://java.sun.com/j2se/1.4.2/docs/api/java/net/ServerSocket.html#ServerSocket( int,%20int,%20java.net.InetAddress) – cletus 2009-01-12 08:32:07
這是implementation從Apache camel項目未來:
/**
* Checks to see if a specific port is available.
*
* @param port the port to check for availability
*/
public static boolean available(int port) {
if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
throw new IllegalArgumentException("Invalid start port: " + port);
}
ServerSocket ss = null;
DatagramSocket ds = null;
try {
ss = new ServerSocket(port);
ss.setReuseAddress(true);
ds = new DatagramSocket(port);
ds.setReuseAddress(true);
return true;
} catch (IOException e) {
} finally {
if (ds != null) {
ds.close();
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
/* should not be thrown */
}
}
}
return false;
}
他們正在檢查的DatagramSocket類以及檢查端口處於UDP和TCP avaliable。
希望這會有所幫助。
在我的情況下,它有助於嘗試連接到端口 - 如果服務已經存在,它會作出響應。
try {
log.debug("{}: Checking if port open by trying to connect as a client", portNumber);
Socket sock = new Socket("localhost", portNumber);
sock.close();
log.debug("{}: Someone responding on port - seems not open", portNumber);
return false;
} catch (Exception e) {
if (e.getMessage().contains("refused")) {
return true;
}
log.error("Troubles checking if port is open", e);
throw new RuntimeException(e);
}
看來,從Java 7開始,David Santamaria's answer不再可靠工作。但是,看起來您仍然可以可靠地使用套接字來測試連接。
private static boolean available(int port) {
System.out.println("--------------Testing port " + port);
Socket s = null;
try {
s = new Socket("localhost", port);
// If the code makes it this far without an exception it means
// something is using the port and has responded.
System.out.println("--------------Port " + port + " is not available");
return false;
} catch (IOException e) {
System.out.println("--------------Port " + port + " is available");
return true;
} finally {
if(s != null){
try {
s.close();
} catch (IOException e) {
throw new RuntimeException("You should handle this error." , e);
}
}
}
}
對於Java 7,你可以使用try-與資源用於更緊湊的代碼:
private static boolean available(int port) {
try (Socket ignored = new Socket("localhost", port)) {
return false;
} catch (IOException ignored) {
return true;
}
}
的try/catch語句插座基礎的解決方案,可能不會產生準確的結果(套接字地址是「本地主機「並且在某些情況下,端口可能不被回送接口」佔用「,並且至少在Windows上我已經看到這個測試失敗,也就是說虛假聲明爲可用)。
有SIGAR名爲酷庫,下面的代碼可以幫你搞到:
Sigar sigar = new Sigar();
int flags = NetFlags.CONN_TCP | NetFlags.CONN_SERVER | NetFlags.CONN_CLIENT; NetConnection[] netConnectionList = sigar.getNetConnectionList(flags);
for (NetConnection netConnection : netConnectionList) {
if (netConnection.getLocalPort() == port)
return false;
}
return true;
我已經試過這樣的事情,它的工作非常細跟我
Socket Skt;
String host = "localhost";
int i = 8983; // port no.
try {
System.out.println("Looking for "+ i);
Skt = new Socket(host, i);
System.out.println("There is a Server on port "
+ i + " of " + host);
}
catch (UnknownHostException e) {
System.out.println("Exception occured"+ e);
}
catch (IOException e) {
System.out.println("port is not used");
}
在我的案例我不得不使用DatagramSocket類。
boolean isPortOccupied(int port) {
DatagramSocket sock = null;
try {
sock = new DatagramSocket(port);
sock.close();
return false;
} catch (BindException ignored) {
return true;
} catch (SocketException ex) {
System.out.println(ex);
return true;
}
}
不要忘了導入第一
import java.net.DatagramSocket;
import java.net.BindException;
import java.net.SocketException;
下面的解決方案是由SocketUtils實現Spring的核心(Apache許可證)的啓發。
與使用Socket(...)
這是相當快的其他解決方案(測試在更短的1000個TCP端口超過一秒):
public static boolean isTcpPortAvailable(int port) {
try (ServerSocket serverSocket = new ServerSocket()) {
// setReuseAddress(false) is required only on OSX,
// otherwise the code will not work correctly on that platform
serverSocket.setReuseAddress(false);
serverSocket.bind(new InetSocketAddress(InetAddress.getByName("localhost"), port), 1);
return true;
} catch (Exception ex) {
return false;
}
}
- 1. 端口轉發使用套接字
- 2. Java套接字 - 套接字端口
- 3. 套接字 - 發現防火牆端口
- 4. Java套接字端口轉發
- 5. 使用Java套接字處理不可達UDP端口
- 6. 使用套接字的端口帶寬
- 7. Python套接字端口使用
- 8. 我可以使用哪個端口用於我的套接字?
- 9. Java - 使用多個數據報套接字和端口
- 10. Java - 套接字編程/阻塞端口
- 11. Java端口套接字編程錯誤
- 12. TCP和UDP套接字可以使用相同的端口嗎?
- 13. TCP異步套接字端口轉發
- 14. 使用套接字io java客戶端的套接字io客戶端
- 15. 套接字調用錯誤的端口
- 16. 可可異步套接字Java實現
- 17. Memcache套接字/端口
- 18. 使用多個端口的套接字客戶端/服務器
- 19. 可通過端口套接字發送的最大數據量
- 20. 在Java中,如何使用HttpURLConnection連接並釋放端口/套接字?
- 21. 使用SSL爲Windows套接字連接保護Windows端口
- 22. 我如何發送和接收同一端口上同時使用套接字
- 23. 用於Java的UNIX套接字實現?
- 24. Java客戶端/服務器套接字端口分配
- 25. 「已連接」UDP套接字接收ICMP端口不可達
- 26. 客戶端 - 服務器Unix套接字和端口轉發
- 27. 使用JSCH(java)在一個封閉的端口下隧道套接字
- 28. Java RMI連接使用什麼端口?
- 29. Java-接口使用接口
- 30. 我可以使用從Java實現java接口的scala類嗎?
雖然這個問題是關於如何知道如果給定的端口已在使用,你可能已經降落在這裏試圖找到一種方法來獲得一個免費的端口號,這http://stackoverflow.com/questions/2675362/how-to-find-an-available-port(連接到我的https://要點.github.com/3429822)涵蓋更好。 – vorburger 2012-08-22 22:12:20
爲了什麼目的?如果你只是想找一個空的套接字來聽,只需指定零。任何掃描技術都有可能出現計時窗口問題:當您掃描時端口可能是空的,並且當您去索賠時佔用端口。 – EJP 2016-02-28 19:38:05