-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkHandler.java
More file actions
42 lines (36 loc) · 1.23 KB
/
Copy pathNetworkHandler.java
File metadata and controls
42 lines (36 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.ashishp.dc.assignment.cl.handler;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author <a href="http://www.linkedin.com/in/aashishpanchal">Aashish</a>
*
*/
public abstract class NetworkHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(NetworkHandler.class);
private static final String LOCALHOST = "127.0.0.1";
/**
* List all possible IPv4 addresses of the current machine
*/
public static void printMachineIPv4() {
try {
LOGGER.info("Printing all possible IPv4 of your machine:");
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
Enumeration<InetAddress> inetAddresses = networkInterfaces.nextElement().getInetAddresses();
while (inetAddresses.hasMoreElements()) {
String hostAddress = inetAddresses.nextElement().getHostAddress();
if (hostAddress.contains(".") && !hostAddress.equals(LOCALHOST)) {
LOGGER.info(hostAddress);
}
}
}
} catch (SocketException e) {
LOGGER.error("Failed to get network interface", e);
}
}
}