Example usage for java.net NetworkInterface getByName

List of usage examples for java.net NetworkInterface getByName

Introduction

In this page you can find the example usage for java.net NetworkInterface getByName.

Prototype

public static NetworkInterface getByName(String name) throws SocketException 

Source Link

Document

Searches for the network interface with the specified name.

Usage

From source file:Main.java

static public void main(String args[]) throws Exception {
    NetworkInterface ni = NetworkInterface.getByName(args[0]);
    System.out.println(ni);// w  ww  .  j a  v a2 s  . c om
}

From source file:Main.java

static public void main(String args[]) throws Exception {
    int port = 80;

    NetworkInterface ni = NetworkInterface.getByName("name");
    Enumeration e = ni.getInetAddresses();
    if (!e.hasMoreElements())
        return;//w w  w .ja  v  a2s  .  co m
    InetAddress ia = (InetAddress) e.nextElement();

    ServerSocket ss = new ServerSocket(port, 20, ia);
    System.out.println("Listening");
    Socket s = ss.accept();
    System.out.println(s);
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    NetworkInterface networkInterface = NetworkInterface.getByName("net1");

    DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET);

    dc.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    dc.bind(new InetSocketAddress(8080));
    dc.setOption(StandardSocketOptions.IP_MULTICAST_IF, networkInterface);

    InetAddress group = InetAddress.getByName("180.90.4.12");
    MembershipKey key = dc.join(group, networkInterface);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DatagramChannel server = DatagramChannel.open();
    server.bind(null);//from   w ww .  j  a  v a2s  . c o  m
    NetworkInterface interf = NetworkInterface.getByName(MULTICAST_INTERFACE_NAME);
    server.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);

    String msg = "Hello!";
    ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
    InetSocketAddress group = new InetSocketAddress(MULTICAST_IP, MULTICAST_PORT);

    server.send(buffer, group);
    System.out.println("Sent the   multicast  message: " + msg);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    MembershipKey key = null;/*from  w w w  . j  a  v  a2  s.  co  m*/
    DatagramChannel client = DatagramChannel.open(StandardProtocolFamily.INET);

    NetworkInterface interf = NetworkInterface.getByName(MULTICAST_INTERFACE_NAME);
    client.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    client.bind(new InetSocketAddress(MULTICAST_PORT));
    client.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);

    InetAddress group = InetAddress.getByName(MULTICAST_IP);
    key = client.join(group, interf);

    System.out.println("Joined the   multicast  group:" + key);
    System.out.println("Waiting for a  message  from  the" + "  multicast group....");

    ByteBuffer buffer = ByteBuffer.allocate(1048);
    client.receive(buffer);
    buffer.flip();
    int limits = buffer.limit();
    byte bytes[] = new byte[limits];
    buffer.get(bytes, 0, limits);
    String msg = new String(bytes);

    System.out.format("Multicast Message:%s%n", msg);
    key.drop();
}

From source file:proxy.ElementalHttpGet.java

static List<InetAddress> getAllIp(String networkInterface) {
    try {//  w w w  .  j a va  2s .  com
        NetworkInterface interfaces = NetworkInterface.getByName(networkInterface);
        Enumeration<InetAddress> inetAddresses = interfaces.getInetAddresses();
        List<InetAddress> result = new ArrayList<InetAddress>();
        while (inetAddresses.hasMoreElements()) {
            InetAddress inetAddress = inetAddresses.nextElement();
            if (inetAddress instanceof Inet4Address) {
                result.add(inetAddress);
            }
        }
        return result;
    } catch (SocketException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.git.original.common.utils.IPUtils.java

/**
 * ?IP?//from   w w w.ja  v  a2  s . c  o  m
 * <p>
 * :<br/>
 * 1. 220.xxx.xxx.xxx<br>
 * 2. 123.xxx.xxx.xxx<br>
 * other<br>
 * 
 * @return
 * @throws SocketException
 *             If an I/O error occurs.
 * @throws NullPointerException
 *             can not found the interface
 * @throws RuntimeException
 *             can not get net address
 */
public static InetAddress getWANIpv4Address(String interfaceName)
        throws SocketException, NullPointerException, RuntimeException {

    InetAddress ipStartWith123 = null;
    InetAddress ipv4Addr = null;

    if (StringUtils.isNotEmpty(interfaceName)) {
        // ?
        NetworkInterface netInterface = NetworkInterface.getByName(interfaceName.trim());
        if (netInterface == null) {
            throw new NullPointerException("can not found the network interface by name: " + interfaceName);
        }

        Enumeration<InetAddress> addrEnum = netInterface.getInetAddresses();
        while (addrEnum.hasMoreElements()) {
            InetAddress addr = addrEnum.nextElement();
            String hostAddr = addr.getHostAddress();

            if (hostAddr.startsWith("220.")) {
                return addr;
            } else if (ipStartWith123 == null && hostAddr.startsWith("123.")) {
                ipStartWith123 = addr;
            } else if (addr instanceof Inet4Address) {
                ipv4Addr = addr;
            }
        }
    } else {
        /*
         * ???
         */
        Enumeration<NetworkInterface> interfaceEnum = NetworkInterface.getNetworkInterfaces();
        while (interfaceEnum.hasMoreElements()) {
            NetworkInterface netInterface = interfaceEnum.nextElement();
            if (netInterface.isLoopback() || !netInterface.isUp()) {
                continue;
            }

            Enumeration<InetAddress> addrEnum = netInterface.getInetAddresses();
            while (addrEnum.hasMoreElements()) {
                InetAddress addr = addrEnum.nextElement();
                String hostAddr = addr.getHostAddress();

                if (hostAddr.startsWith("220.")) {
                    return addr;
                } else if (ipStartWith123 == null && hostAddr.startsWith("123.")) {
                    ipStartWith123 = addr;
                } else if (addr instanceof Inet4Address) {
                    ipv4Addr = addr;
                }
            }
        }
    }

    if (ipStartWith123 != null) {
        return ipStartWith123;
    } else if (ipv4Addr != null) {
        return ipv4Addr;
    }

    throw new RuntimeException("can not get WAN Address");
}

From source file:uk.co.propter.sleeponlan.SleepService.java

private void discoverMACAddress() {
    try {//from  ww w .  j a v  a  2 s. c o  m
        NetworkInterface ni = NetworkInterface.getByName(networkInterface);
        macAddress = ni.getHardwareAddress();
    } catch (SocketException e) {
        logger.severe("Could not discover MAC address.");
        System.exit(1);
    }
}

From source file:com.chiralBehaviors.slp.hive.configuration.MulticastConfiguration.java

public NetworkInterface getNetworkInterface() throws SocketException {
    if (networkInterface == null) {
        for (Enumeration<NetworkInterface> intfs = NetworkInterface.getNetworkInterfaces(); intfs
                .hasMoreElements();) {// w w  w.  j av a2  s.  c o m
            NetworkInterface intf = intfs.nextElement();
            if (intf.supportsMulticast()) {
                return intf;
            }
        }
        throw new IllegalStateException("No interface supporting multicast was discovered");
    }
    NetworkInterface iface = NetworkInterface.getByName(networkInterface);
    if (iface == null) {
        throw new IllegalArgumentException(
                String.format("Cannot find network interface: %s ", networkInterface));
    }
    return iface;
}

From source file:com.smartdevicelink.tcpdiscovery.DiscoveredDevice.java

private String getLocalAddressForIface(String ifaceName) throws Exception {
    NetworkInterface iface = NetworkInterface.getByName(ifaceName);
    Enumeration<InetAddress> inetAddresses = iface.getInetAddresses();
    for (InetAddress inetAddress : Collections.list(inetAddresses)) {
        if (InetAddressUtils.isIPv4Address(inetAddress.toString())) {
            return inetAddress.toString();
        }//www  . ja va2  s . co  m
    }
    return "";
}