use of java.net.NetworkInterface in project pinpoint by naver.
the class LocalHostTest method portName.
@Ignore
@Test
public void portName() throws UnknownHostException, SocketException {
logger.debug("CanonicalHostName:{}", InetAddress.getLocalHost().getCanonicalHostName());
logger.debug("HostName:{}", InetAddress.getLocalHost().getHostName());
logger.debug("NetworkInterface");
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
logger.debug("NetworkInterface:{}", networkInterface);
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
logger.debug(inetAddress.getCanonicalHostName());
logger.debug(inetAddress.getHostName());
}
}
}
use of java.net.NetworkInterface in project neo4j by neo4j.
the class DefaultUdcInformationCollector method determineMacAddress.
private String determineMacAddress() {
String formattedMac = "0";
try {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
if (ni != null) {
byte[] mac = ni.getHardwareAddress();
if (mac != null) {
StringBuilder sb = new StringBuilder(mac.length * 2);
Formatter formatter = new Formatter(sb);
for (byte b : mac) {
formatter.format("%02x", b);
}
formattedMac = sb.toString();
}
}
} catch (Throwable t) {
//
}
return formattedMac;
}
use of java.net.NetworkInterface in project openhab1-addons by openhab.
the class SsdpDiscovery method getBroadCastAddress.
private static List<InetAddress> getBroadCastAddress() throws Exception {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
List<InetAddress> addresses = new ArrayList<InetAddress>();
addresses.add(InetAddress.getByName("255.255.255.255"));
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
if (networkInterface.isLoopback() || !networkInterface.supportsMulticast()) {
// Don't want to broadcast to the loopback interface
continue;
}
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
InetAddress broadcast = interfaceAddress.getBroadcast();
if (broadcast != null) {
addresses.add(broadcast);
}
}
}
return addresses;
}
use of java.net.NetworkInterface in project openhab1-addons by openhab.
the class MaxCubeDiscover method discoverIp.
/**
* Automatic UDP discovery of a MAX!Cube
*
* @return if the cube is found, returns the IP address as a string. Otherwise returns null
*/
public static final String discoverIp() {
String maxCubeIP = null;
String maxCubeName = null;
String rfAddress = null;
Logger logger = LoggerFactory.getLogger(MaxCubeDiscover.class);
DatagramSocket bcReceipt = null;
DatagramSocket bcSend = null;
// Find the MaxCube using UDP broadcast
try {
bcSend = new DatagramSocket();
bcSend.setBroadcast(true);
byte[] sendData = "eQ3Max*\0**********I".getBytes();
// Broadcast the message over all the network interfaces
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
if (networkInterface.isLoopback() || !networkInterface.isUp()) {
continue;
}
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
InetAddress[] broadcast = new InetAddress[2];
broadcast[0] = InetAddress.getByName("224.0.0.1");
broadcast[1] = interfaceAddress.getBroadcast();
for (InetAddress bc : broadcast) {
// Send the broadcast package!
if (bc != null) {
try {
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, bc, 23272);
bcSend.send(sendPacket);
} catch (IOException e) {
logger.debug("IO error during MAX! Cube discovery: {}", e.getMessage());
} catch (Exception e) {
logger.debug(e.getMessage());
logger.debug(Utils.getStackTrace(e));
}
logger.trace("Request packet sent to: {} Interface: {}", bc.getHostAddress(), networkInterface.getDisplayName());
}
}
}
}
logger.trace("Done looping over all network interfaces. Now waiting for a reply!");
bcSend.close();
bcReceipt = new DatagramSocket(23272);
bcReceipt.setReuseAddress(true);
// Wait for a response
byte[] recvBuf = new byte[15000];
DatagramPacket receivePacket = new DatagramPacket(recvBuf, recvBuf.length);
bcReceipt.receive(receivePacket);
// We have a response
logger.trace("Broadcast response from server: {}", receivePacket.getAddress());
// Check if the message is correct
String message = new String(receivePacket.getData()).trim();
if (message.startsWith("eQ3Max")) {
maxCubeIP = receivePacket.getAddress().getHostAddress();
maxCubeName = message.substring(0, 8);
rfAddress = message.substring(8, 18);
logger.debug("Found at: {}", maxCubeIP);
logger.debug("Name : {}", maxCubeName);
logger.debug("Serial : {}", rfAddress);
logger.trace("Message : {}", message);
} else {
logger.info("No Max!Cube gateway found on network");
}
} catch (IOException e) {
logger.debug("IO error during MAX! Cube discovery: {}", e.getMessage());
} catch (Exception e) {
logger.debug(e.getMessage());
logger.debug(Utils.getStackTrace(e));
} finally {
try {
if (bcReceipt != null) {
bcReceipt.close();
}
} catch (Exception e) {
logger.debug(e.toString());
}
try {
if (bcSend != null) {
bcSend.close();
}
} catch (Exception e) {
logger.debug(e.toString());
}
}
return maxCubeIP;
}
use of java.net.NetworkInterface in project openhab1-addons by openhab.
the class HarmonyHubDiscovery method sendDiscoveryMessage.
/**
* Send broadcast message over all active interfaces
*
* @param discoverString
* String to be used for the discovery
*/
private void sendDiscoveryMessage(String discoverString) {
DatagramSocket bcSend = null;
try {
bcSend = new DatagramSocket();
bcSend.setBroadcast(true);
byte[] sendData = discoverString.getBytes();
// Broadcast the message over all the network interfaces
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
if (networkInterface.isLoopback() || !networkInterface.isUp()) {
continue;
}
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
InetAddress[] broadcast = new InetAddress[3];
broadcast[0] = InetAddress.getByName("224.0.0.1");
broadcast[1] = InetAddress.getByName("255.255.255.255");
broadcast[2] = interfaceAddress.getBroadcast();
broadcast[3] = InetAddress.getByName(optionalHost);
for (InetAddress bc : broadcast) {
// Send the broadcast package!
if (bc != null && !bc.isLoopbackAddress()) {
try {
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, bc, DISCO_PORT);
bcSend.send(sendPacket);
} catch (IOException e) {
logger.error("IO error during HarmonyHub discovery: {}", e.getMessage());
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
logger.trace("Request packet sent to: {} Interface: {}", bc.getHostAddress(), networkInterface.getDisplayName());
}
}
}
}
} catch (IOException e) {
logger.debug("IO error during HarmonyHub discovery: {}", e.getMessage());
} finally {
try {
if (bcSend != null) {
bcSend.close();
}
} catch (Exception e) {
// Ignore
}
}
}
Aggregations