Ejemplo de Java: comprobar si el puerto está en uso
El siguiente ejemplo demuestra cómo comprobar si un puerto ya está en uso:
Ejemplo
import java.net.*;
import java.io.*;
public class Main {
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("Comprobar"+ i);
Skt = new Socket(host, i);
System.out.println("Puerto" + i + "Ya está en uso");
}
catch (UnknownHostException e) {
System.out.println("Exception occured"+ e);
break;
}
catch (IOException e) {
}
}
}
}
El resultado de ejecutar el código anterior es:
…… 查看 17 查看 18 查看 19 查看 20 查看 21 端口 21 已被使用 查看 22 查看 23 查看 24 ……
También puede especificar el puerto del host:
Archivo 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) {
//Comprobar el puerto 80 local
log(isSocketAliveUitlitybyCrunchify("localhost", 80));
//Comprobar el puerto 8080 local
log(isSocketAliveUitlitybyCrunchify("localhost", 8080));
//Comprobar el puerto 8081 local
log(isSocketAliveUitlitybyCrunchify("localhost", 8081));
//Comprobar el puerto 80 de example.com
log(isSocketAliveUitlitybyCrunchify("example.com", 80));
//Comprobar el puerto 443 de example.com
log(isSocketAliveUitlitybyCrunchify("example.com", 443));
//Comprobar el puerto 81 de example.com
log(isSocketAliveUitlitybyCrunchify("example.com", 81));
}
/**
* Comprueba el puerto del host
*
* @param hostName
* @param port
* @return boolean - true/false
*/
public static boolean isSocketAliveUitlitybyCrunchify(String hostName, int port) {
boolean isAlive = false;
//Crear un socket
SocketAddress socketAddress = new InetSocketAddress(hostName, port);
Socket socket = new Socket();
//Configuración de tiempo de espera, en milisegundos
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("Si realmente está en uso:" + isAlive + "\n");
}
}
El resultado de ejecutar el código anterior es:
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: example.com, port: 80 是否真正在使用: true hostName: example.com, port: 443 是否真正在使用: true hostName: example.com, port: 81 SocketTimeoutException example.com:81. connect timed out 是否真正在使用: falseOtras extensiones
Ejemplo de Java