Search in sources :

Example 31 with NetworkInterface

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());
        }
    }
}
Also used : NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 32 with NetworkInterface

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;
}
Also used : Formatter(java.util.Formatter) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 33 with NetworkInterface

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;
}
Also used : InterfaceAddress(java.net.InterfaceAddress) ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 34 with NetworkInterface

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;
}
Also used : DatagramSocket(java.net.DatagramSocket) InterfaceAddress(java.net.InterfaceAddress) DatagramPacket(java.net.DatagramPacket) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) Logger(org.slf4j.Logger) InetAddress(java.net.InetAddress) IOException(java.io.IOException)

Example 35 with NetworkInterface

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
        }
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) InterfaceAddress(java.net.InterfaceAddress) DatagramPacket(java.net.DatagramPacket) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) InetAddress(java.net.InetAddress) IOException(java.io.IOException)

Aggregations

NetworkInterface (java.net.NetworkInterface)225 InetAddress (java.net.InetAddress)178 SocketException (java.net.SocketException)112 ArrayList (java.util.ArrayList)42 Inet4Address (java.net.Inet4Address)39 UnknownHostException (java.net.UnknownHostException)36 InterfaceAddress (java.net.InterfaceAddress)32 Inet6Address (java.net.Inet6Address)29 IOException (java.io.IOException)25 Enumeration (java.util.Enumeration)9 InetSocketAddress (java.net.InetSocketAddress)7 HashSet (java.util.HashSet)7 LinkedHashSet (java.util.LinkedHashSet)7 Command (com.android.server.NativeDaemonConnector.Command)6 Test (org.junit.Test)6 DatagramSocket (java.net.DatagramSocket)5 File (java.io.File)4 MulticastSocket (java.net.MulticastSocket)4 List (java.util.List)4 SharedPreferences (android.content.SharedPreferences)3