Class with methods to check if enterde ip and port are valid.
This commit is contained in:
41
src/main/java/java/src/Input/NetworkController.java
Normal file
41
src/main/java/java/src/Input/NetworkController.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package java.src.Input;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.ServerSocket;
|
||||
import java.util.Enumeration;
|
||||
|
||||
public class NetworkController {
|
||||
public static boolean hasNetworkInterfaceWithIP(String ipAddressAsString) {
|
||||
try {
|
||||
InetAddress ipAddress = InetAddress.getByName(ipAddressAsString);
|
||||
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
|
||||
|
||||
while (networkInterfaces.hasMoreElements()) {
|
||||
NetworkInterface networkInterface = networkInterfaces.nextElement();
|
||||
Enumeration<InetAddress> interfaceAddresses = networkInterface.getInetAddresses();
|
||||
|
||||
while (interfaceAddresses.hasMoreElements()) {
|
||||
InetAddress address = interfaceAddresses.nextElement();
|
||||
if (address.equals(ipAddress)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// No network interface found with the specified IP
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isPortFree(int port) {
|
||||
try (ServerSocket serverSocket = new ServerSocket(port)) {
|
||||
// If the ServerSocket can be created without exception, the port is free
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
// If an exception occurs, it means the port is already in use
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user