public static List<String> getLocalInetAddress() { List<String> inetAddressList = new ArrayList<String>(); try { Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); while (enumeration.hasMoreElements()) { NetworkInterface networkInterface = enumeration.nextElement(); Enumeration<InetAddress> addrs = networkInterface.getInetAddresses(); while (addrs.hasMoreElements()) { inetAddressList.add(addrs.nextElement().getHostAddress()); } } } catch (SocketException e) { throw new RuntimeException("get local inet address fail", e); } return inetAddressList; }
public static String getLocalhostByNetworkInterface() throws SocketException { List<String> candidatesHost = new ArrayList<String>(); Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); while (enumeration.hasMoreElements()) { NetworkInterface networkInterface = enumeration.nextElement(); // Workaround for docker0 bridge if ("docker0".equals(networkInterface.getName()) || !networkInterface.isUp()) { continue; } Enumeration<InetAddress> addrs = networkInterface.getInetAddresses(); while (addrs.hasMoreElements()) { InetAddress address = addrs.nextElement(); if (address.isLoopbackAddress()) { continue; } //ip4 highter priority if (address instanceof Inet6Address) { candidatesHost.add(address.getHostAddress()); continue; } return address.getHostAddress(); } } if (!candidatesHost.isEmpty()) { return candidatesHost.get(0); } return null; }
@Override public ChannelFuture joinGroup(InetAddress multicastAddress, ChannelPromise promise) { try { return joinGroup( multicastAddress, NetworkInterface.getByInetAddress(localAddress().getAddress()), null, promise); } catch (SocketException e) { promise.setFailure(e); } return promise; }
private static boolean isMulticastEnabled(Collection<NetworkInterface> interfaces) { for (NetworkInterface iface : interfaces) { try { if (iface.isUp() && (iface.supportsMulticast() || iface.isLoopback())) { return true; } } catch (SocketException e) { // Ignore } } return false; }
private static boolean isIfaceConnected(NetworkInterface networkInterface) { try { return networkInterface.isUp() && !networkInterface.isLoopback() && !networkInterface.getInterfaceAddresses().isEmpty(); } catch (SocketException e) { return false; } }
public static void setInterface (MulticastSocket multicastSocket, boolean preferIpv6) throws IOException{ boolean interfaceSet = false; Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface i = (NetworkInterface) interfaces.nextElement(); Enumeration addresses = i.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = (InetAddress) addresses.nextElement(); if (preferIpv6 && address instanceof Inet6Address) { multicastSocket.setInterface(address); interfaceSet = true; break; } else if (!preferIpv6 && address instanceof Inet4Address) { multicastSocket.setInterface(address); interfaceSet = true; break; } } if (interfaceSet) { break; } } }
protected void bindToInterfaces(List<NetworkInterface> interfaces, MulticastSocket s, InetAddress mcast_addr) throws IOException { SocketAddress tmp_mcast_addr=new InetSocketAddress(mcast_addr, mcast_port); for(Iterator it=interfaces.iterator(); it.hasNext();) { NetworkInterface i=(NetworkInterface)it.next(); for(Enumeration en2=i.getInetAddresses(); en2.hasMoreElements();) { InetAddress addr=(InetAddress)en2.nextElement(); if ((Util.getIpStackType() == StackType.IPv4 && addr instanceof Inet4Address) || (Util.getIpStackType() == StackType.IPv6 && addr instanceof Inet6Address)) { s.joinGroup(tmp_mcast_addr, i); log.trace("joined " + tmp_mcast_addr + " on " + i.getName() + " (" + addr + ")"); break; } } } }
@CheckForNull private static byte[] getMacAddress() throws SocketException { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); if (en != null) { while (en.hasMoreElements()) { NetworkInterface nint = en.nextElement(); if (!nint.isLoopback()) { // Pick the first valid non loopback address we find byte[] address = nint.getHardwareAddress(); if (isValidAddress(address)) { return address; } } } } // Could not find a mac address return null; }
bind_addr=InetAddress.getByName(args[++i]); continue; mcast_addr=InetAddress.getByName(args[++i]); continue; mcast_addr=InetAddress.getByName("224.0.1.105"); sock=new MulticastSocket(port); SocketAddress join_addr=new InetSocketAddress(mcast_addr, port); for(Enumeration en=NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { intf=(NetworkInterface)en.nextElement(); sock.joinGroup(join_addr, intf); System.out.println("joined " + join_addr + " on " + intf.getName()); sock.setInterface(bind_addr); sock.joinGroup(join_addr, null);
} else { try { final Enumeration<InetAddress> e = NetworkInterface.getByName(iface).getInetAddresses(); while (e.hasMoreElements()) { server.port(new ServerPort(new InetSocketAddress(e.nextElement(), port), protocols)); final byte[] bytes = NetUtil.createByteArrayFromIpAddressString(ip); try { server.port(new ServerPort(new InetSocketAddress( InetAddress.getByAddress(bytes), port), protocols)); } catch (UnknownHostException e) {
Enumeration<NetworkInterface> ifs = null; InetSocketAddress socketAddress = new InetSocketAddress(Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT); try { ifs = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { log.error("Could not get network interfaces for this machine: " + e.getMessage()); while (ifs.hasMoreElements()) { NetworkInterface xface = ifs.nextElement(); Enumeration<InetAddress> addrs = xface.getInetAddresses(); String name = xface.getName(); int IPsPerNic = 0; while (addrs.hasMoreElements()) { IPsPerNic++; log.debug("Adding " + name + " to our interface set"); } catch (IOException e) { log.warn("Multicast join failed for: " + socketAddress.getHostName() + " to interface: " + xface.getName() + " with message: " + e.getMessage()); sendUpnpNotify(socketAddress.getAddress()); previous = Instant.now();
private static InetAddress getLocalAddress0() { InetAddress localAddress = null; try { localAddress = InetAddress.getLocalHost(); Optional<InetAddress> addressOp = toValidAddress(localAddress); if (addressOp.isPresent()) { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); if (null == interfaces) { return localAddress; while (interfaces.hasMoreElements()) { try { NetworkInterface network = interfaces.nextElement(); Enumeration<InetAddress> addresses = network.getInetAddresses(); while (addresses.hasMoreElements()) { try { Optional<InetAddress> addressOp = toValidAddress(addresses.nextElement());
private Inet6Address getLocalIPv6Address() { try { Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface netInterface = e.nextElement(); Enumeration<InetAddress> ee = netInterface.getInetAddresses(); while (ee.hasMoreElements()) { InetAddress addr = ee.nextElement(); if (addr instanceof Inet6Address && (!addr.isLoopbackAddress()) && (!addr.isAnyLocalAddress())) { InetSocketAddress socketAddress = new InetSocketAddress(addr, 0); ActorSystem as = AkkaUtils.createActorSystem( new Configuration(), new Some<scala.Tuple2<String, Object>>(new scala.Tuple2<String, Object>(addr.getHostAddress(), port))); as.shutdown();
private String getDefaultAddress() { Enumeration<NetworkInterface> nets; try { nets = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { return null; } NetworkInterface netinf; while (nets.hasMoreElements()) { netinf = nets.nextElement(); Enumeration<InetAddress> addresses = netinf.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (!address.isAnyLocalAddress() && !address.isMulticastAddress() && !(address instanceof Inet6Address)) { return address.getHostAddress(); } } } return null; }
String ip; try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); // filters out 127.0.0.1 and inactive interfaces if (iface.isLoopback() || !iface.isUp()) continue; Enumeration<InetAddress> addresses = iface.getInetAddresses(); while(addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); ip = addr.getHostAddress(); System.out.println(iface.getDisplayName() + " " + ip); } } } catch (SocketException e) { throw new RuntimeException(e); }
bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.keepAlive", true); Channel listenChannel = bootstrap.bind(new InetSocketAddress(port)); allChannels.add(listenChannel); logger.info("Started server on port {}", port); Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface ifc = interfaces.nextElement(); if (!ifc.isLoopback()) { for (InterfaceAddress ifadr : ifc.getInterfaceAddresses()) { InetAddress adr = ifadr.getAddress(); logger.info("Listening on network interface \"{}\", hostname \"{}\" ({})", ifc.getDisplayName(), adr.getCanonicalHostName(), adr.getHostAddress());
String bindAddress = null; Enumeration<NetworkInterface> intfs = NetworkInterface.getNetworkInterfaces(); while(intfs.hasMoreElements()) { NetworkInterface i = intfs.nextElement(); try { if (i.isLoopback()) { Enumeration<InetAddress> addrs = i.getInetAddresses(); while (addrs.hasMoreElements()) { InetAddress a = addrs.nextElement(); if(a.isLoopbackAddress()) { bindAddress = a.getHostAddress(); if (a instanceof Inet6Address) { bindAddress = "[" + bindAddress + "]"; new InetSocketAddress(bindAddress, PORT), -1); f.startup(zks); LOG.info("starting up the the server, waiting");
public static void main(String[] args) throws SocketException { Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces(); while(en.hasMoreElements()) { NetworkInterface i=en.nextElement(); System.out.println(i.getName() + ':'); System.out.println(" \t" + i.getDisplayName()); for(Enumeration<InetAddress> en2=i.getInetAddresses(); en2.hasMoreElements();) { InetAddress addr=en2.nextElement(); System.out.println(" \t" + addr + " (" + addr.getHostName() + ')'); } System.out.println("---------------------"); } }
MulticastSocket upnpMulticastSocket = new MulticastSocket(UPNP_DISCOVERY_PORT);) { InetSocketAddress socketAddress = new InetSocketAddress(UPNP_MULTICAST_ADDRESS, UPNP_DISCOVERY_PORT); Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces(); while (ifs.hasMoreElements()) { NetworkInterface xface = ifs.nextElement(); Enumeration<InetAddress> addrs = xface.getInetAddresses(); String name = xface.getName(); int IPsPerNic = 0; while (addrs.hasMoreElements()) { InetAddress addr = addrs.nextElement(); log.debug(name + " ... has addr " + addr); if (InetAddressUtils.isIPv4Address(addr.getHostAddress())) { IPsPerNic++; upnpMulticastSocket.joinGroup(socketAddress, xface); log.debug("Adding " + name + " to our interface set"); byte[] buf = new byte[1024]; DatagramPacket packet = new DatagramPacket(buf, buf.length); upnpMulticastSocket.receive(packet); String packetString = new String(packet.getData()); if(isSSDPDiscovery(packetString)){ log.debug("Got SSDP Discovery packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort()); for(int i = 0; i < portCount; i ++) { sendUpnpResponse(responseSocket, packet.getAddress(), packet.getPort(), portBase+i, i);
private static InetAddress getIpAddress(AddressSelectionCondition condition) throws SocketException { // Before we connect somewhere, we cannot be sure about what we'd be bound to; however, // we only connect when the message where client ID is, is long constructed. Thus, // just use whichever IP address we can find. Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface current = interfaces.nextElement(); if (!current.isUp() || current.isLoopback() || current.isVirtual()) continue; Enumeration<InetAddress> addresses = current.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr.isLoopbackAddress()) continue; if (condition.isAcceptableAddress(addr)) { return addr; } } } throw new SocketException("Can't get our ip address, interfaces are: " + interfaces); }