Java 类java.net.InterfaceAddress 实例源码
项目:ja-micro
文件:RegistrationManager.java
private void updateIpAddress() {
try {
Enumeration<NetworkInterface> b = NetworkInterface.getNetworkInterfaces();
ipAddress = null;
while( b.hasMoreElements()){
NetworkInterface iface = b.nextElement();
if (iface.getName().startsWith("dock")) {
continue;
}
for ( InterfaceAddress f : iface.getInterfaceAddresses()) {
if (f.getAddress().isSiteLocalAddress()) {
ipAddress = f.getAddress().getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
项目:jdk8u-jdk
文件:NetworkPrefixLength.java
public static void main(String[] args) throws Exception {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
while (nics.hasMoreElements()) {
NetworkInterface nic = nics.nextElement();
for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
boolean valid = checkPrefix(iaddr);
if (!valid) {
passed = false;
debug(nic.getName(), iaddr);
}
InetAddress ia = iaddr.getAddress();
if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
// assumption: prefix length will always be 8
if (iaddr.getNetworkPrefixLength() != 8) {
out.println("Expected prefix of 8, got " + iaddr);
passed = false;
}
}
}
}
if (!passed)
throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
项目:openjdk-jdk10
文件:NetworkPrefixLength.java
public static void main(String[] args) throws Exception {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
while (nics.hasMoreElements()) {
NetworkInterface nic = nics.nextElement();
for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
boolean valid = checkPrefix(iaddr);
if (!valid) {
passed = false;
debug(nic.getName(), iaddr);
}
InetAddress ia = iaddr.getAddress();
if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
// assumption: prefix length will always be 8
if (iaddr.getNetworkPrefixLength() != 8) {
out.println("Expected prefix of 8, got " + iaddr);
passed = false;
}
}
}
}
if (!passed)
throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
项目:samba-documents-provider
文件:BroadcastUtils.java
static List<String> getBroadcastAddress() throws BrowsingException, SocketException {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
List<String> broadcastAddresses = new ArrayList<>();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
if (networkInterface.isLoopback()) {
continue;
}
for (InterfaceAddress interfaceAddress :
networkInterface.getInterfaceAddresses()) {
InetAddress broadcast = interfaceAddress.getBroadcast();
if (broadcast != null) {
broadcastAddresses.add(broadcast.toString().substring(1));
}
}
}
return broadcastAddresses;
}
项目:openjdk9
文件:NetworkPrefixLength.java
public static void main(String[] args) throws Exception {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
while (nics.hasMoreElements()) {
NetworkInterface nic = nics.nextElement();
for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
boolean valid = checkPrefix(iaddr);
if (!valid) {
passed = false;
debug(nic.getName(), iaddr);
}
InetAddress ia = iaddr.getAddress();
if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
// assumption: prefix length will always be 8
if (iaddr.getNetworkPrefixLength() != 8) {
out.println("Expected prefix of 8, got " + iaddr);
passed = false;
}
}
}
}
if (!passed)
throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
项目:rapid-linux
文件:Util.java
public static InetAddress getBroadcast(InetAddress myIpAddress) {
NetworkInterface temp;
InetAddress iAddr = null;
try {
temp = NetworkInterface.getByInetAddress(myIpAddress);
List<InterfaceAddress> addresses = temp.getInterfaceAddresses();
for (InterfaceAddress inetAddress : addresses) {
iAddr = inetAddress.getBroadcast();
}
System.out.println("iAddr=" + iAddr);
return iAddr;
} catch (SocketException e) {
e.printStackTrace();
System.out.println("getBroadcast" + e.getMessage());
}
return null;
}
项目:cosmic
文件:NetUtils.java
public static String[] getLocalCidrs() {
final String defaultHostIp = getDefaultHostIp();
final List<String> cidrList = new ArrayList<>();
try {
for (final NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) {
if (ifc.isUp() && !ifc.isVirtual() && !ifc.isLoopback()) {
for (final InterfaceAddress address : ifc.getInterfaceAddresses()) {
final InetAddress addr = address.getAddress();
final int prefixLength = address.getNetworkPrefixLength();
if (prefixLength < MAX_CIDR && prefixLength > 0) {
final String ip = addr.getHostAddress();
if (ip.equalsIgnoreCase(defaultHostIp)) {
cidrList.add(ipAndNetMaskToCidr(ip, getCidrNetmask(prefixLength)));
}
}
}
}
}
} catch (final SocketException e) {
s_logger.warn("UnknownHostException in getLocalCidrs().", e);
}
return cidrList.toArray(new String[0]);
}
项目:jdk8u_jdk
文件:NetworkPrefixLength.java
public static void main(String[] args) throws Exception {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
while (nics.hasMoreElements()) {
NetworkInterface nic = nics.nextElement();
for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
boolean valid = checkPrefix(iaddr);
if (!valid) {
passed = false;
debug(nic.getName(), iaddr);
}
InetAddress ia = iaddr.getAddress();
if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
// assumption: prefix length will always be 8
if (iaddr.getNetworkPrefixLength() != 8) {
out.println("Expected prefix of 8, got " + iaddr);
passed = false;
}
}
}
}
if (!passed)
throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
项目:jrpc
文件:NetworkAddressUtil.java
public static final List<InetAddress> getPrivateInetInetAddress() throws SocketException {
List<InetAddress> inetAddresses = new LinkedList<>();
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
NetworkInterface netInterface = e.nextElement();
if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
continue;
}
for (InterfaceAddress address : netInterface.getInterfaceAddresses()) {
InetAddress inetAddress = address.getAddress();
if (inetAddress.isSiteLocalAddress() && !inetAddress.isLoopbackAddress()
&& (inetAddress instanceof Inet4Address)) {
inetAddresses.add(inetAddress);
}
}
}
return inetAddresses;
}
项目:jrpc
文件:NetworkAddressUtil.java
public static final List<InetAddress> getPublicInetInetAddress() throws SocketException {
List<InetAddress> inetAddresses = new LinkedList<>();
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
NetworkInterface netInterface = e.nextElement();
if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
continue;
}
for (InterfaceAddress address : netInterface.getInterfaceAddresses()) {
InetAddress inetAddress = address.getAddress();
if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()
&& !inetAddress.isSiteLocalAddress() && (inetAddress instanceof Inet4Address)) {
inetAddresses.add(inetAddress);
}
}
}
return inetAddresses;
}
项目:lookaside_java-1.8.0-openjdk
文件:NetworkPrefixLength.java
public static void main(String[] args) throws Exception {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
while (nics.hasMoreElements()) {
NetworkInterface nic = nics.nextElement();
for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
boolean valid = checkPrefix(iaddr);
if (!valid) {
passed = false;
debug(nic.getName(), iaddr);
}
InetAddress ia = iaddr.getAddress();
if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
// assumption: prefix length will always be 8
if (iaddr.getNetworkPrefixLength() != 8) {
out.println("Expected prefix of 8, got " + iaddr);
passed = false;
}
}
}
}
if (!passed)
throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
项目:crail
文件:TcpStorageServer.java
public static InetSocketAddress getDataNodeAddress() throws IOException {
String ifname = TcpStorageConstants.STORAGE_TCP_INTERFACE;
int port = TcpStorageConstants.STORAGE_TCP_PORT;
NetworkInterface netif = NetworkInterface.getByName(ifname);
if (netif == null){
return null;
}
List<InterfaceAddress> addresses = netif.getInterfaceAddresses();
InetAddress addr = null;
for (InterfaceAddress address: addresses){
if (address.getBroadcast() != null){
InetAddress _addr = address.getAddress();
addr = _addr;
}
}
InetSocketAddress inetAddr = new InetSocketAddress(addr, port);
return inetAddr;
}
项目:crail
文件:RdmaStorageServer.java
public static InetSocketAddress getDataNodeAddress() throws IOException {
String ifname = RdmaConstants.STORAGE_RDMA_INTERFACE;
int port = RdmaConstants.STORAGE_RDMA_PORT;
NetworkInterface netif = NetworkInterface.getByName(ifname);
if (netif == null){
return null;
}
List<InterfaceAddress> addresses = netif.getInterfaceAddresses();
InetAddress addr = null;
for (InterfaceAddress address: addresses){
// LOG.info("address* " + address.toString() + ", _addr " + _addr.toString() + ", isSiteLocal " + _addr.isSiteLocalAddress() + ", tmp " + tmp + ", size " + tmp.length + ", broadcast " + address.getBroadcast());
if (address.getBroadcast() != null){
InetAddress _addr = address.getAddress();
addr = _addr;
}
}
InetSocketAddress inetAddr = new InetSocketAddress(addr, port);
return inetAddr;
}
项目:Tuntuni
文件:Subnet.java
private void sendBroadcastRequest(InterfaceAddress ia) {
try {
// add to my address list
myAddress.add(ia.getAddress().getHostAddress());
// data to send
byte[] sendData = Commons.toBytes(mDataToSend);
// Send the broadcast package!
for (int port : SubnetServer.PORTS) {
DatagramPacket sendPacket = new DatagramPacket(
sendData, sendData.length,
ia.getBroadcast(), port);
mSocket.send(sendPacket);
}
} catch (Exception ex) {
Logs.error(getClass(), "Failed to send broadcast. {0}", ex);
}
}
项目:little_mitm
文件:NetworkUtils.java
/**
* @deprecated This method is no longer used by LittleProxy and may be removed in a future release.
*/
@Deprecated
public static InetAddress firstLocalNonLoopbackIpv4Address() {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces
.nextElement();
if (networkInterface.isUp()) {
for (InterfaceAddress ifAddress : networkInterface
.getInterfaceAddresses()) {
if (ifAddress.getNetworkPrefixLength() > 0
&& ifAddress.getNetworkPrefixLength() <= 32
&& !ifAddress.getAddress().isLoopbackAddress()) {
return ifAddress.getAddress();
}
}
}
}
return null;
} catch (SocketException se) {
return null;
}
}
项目:offloading-framework-android
文件:Utils.java
public static InetAddress getBroadcast(InetAddress myIpAddress) {
NetworkInterface temp;
InetAddress iAddr = null;
try {
temp = NetworkInterface.getByInetAddress(myIpAddress);
List<InterfaceAddress> addresses = temp.getInterfaceAddresses();
for (InterfaceAddress inetAddress : addresses) {
iAddr = inetAddress.getBroadcast();
}
Log.d(TAG, "iAddr=" + iAddr);
return iAddr;
} catch (SocketException e) {
e.printStackTrace();
Log.d(TAG, "getBroadcast" + e.getMessage());
}
return null;
}
项目:x-pipe
文件:IpUtils.java
protected static List<InetAddress> getAllServerAddress() {
List<InetAddress> result = new LinkedList<>();
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
if (interfaces == null) {
return null;
}
while (interfaces.hasMoreElements()) {
NetworkInterface current = interfaces.nextElement();
List<InterfaceAddress> addresses = current.getInterfaceAddresses();
if (addresses.size() == 0) {
continue;
}
for (InterfaceAddress interfaceAddress : addresses) {
InetAddress address = interfaceAddress.getAddress();
result.add(address);
}
}
} catch (SocketException e) {
}
return result;
}
项目:FlyWoo
文件:WifiUtils.java
public static String getBroadcastAddress() {
System.setProperty("java.net.preferIPv4Stack", "true");
try {
for (Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces(); niEnum
.hasMoreElements(); ) {
NetworkInterface ni = niEnum.nextElement();
if (!ni.isLoopback()) {
for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
if (interfaceAddress.getBroadcast() != null) {
L.d(TAG, interfaceAddress.getBroadcast().toString().substring(1));
return interfaceAddress.getBroadcast().toString().substring(1);
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return null;
}
项目:swingsane
文件:DiscoveryJob.java
public final InetAddress getLocalAddress() throws SocketException {
for (final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces
.hasMoreElements();) {
final NetworkInterface networkInterface = interfaces.nextElement();
if (networkInterface.isLoopback()) {
continue;
}
for (final InterfaceAddress interfaceAddr : networkInterface.getInterfaceAddresses()) {
final InetAddress inetAddr = interfaceAddr.getAddress();
if (!(inetAddr instanceof Inet4Address)) {
continue;
}
return inetAddr;
}
}
return null;
}
项目:LittleProxy
文件:NetworkUtils.java
/**
* Go through our network interfaces and find the first bound address for an
* up interface that's in the IPv4 address space and is not the loopback
* address.
*
* @return
*/
public static InetAddress firstLocalNonLoopbackIpv4Address() {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces
.nextElement();
if (networkInterface.isUp()) {
for (InterfaceAddress ifAddress : networkInterface
.getInterfaceAddresses()) {
if (ifAddress.getNetworkPrefixLength() > 0
&& ifAddress.getNetworkPrefixLength() <= 32
&& !ifAddress.getAddress().isLoopbackAddress()) {
return ifAddress.getAddress();
}
}
}
}
return null;
} catch (SocketException se) {
return null;
}
}
项目:universal-java-matrix-package
文件:NetworkInterfaceMatrix.java
public NetworkInterfaceMatrix(final NetworkInterface networkInterface) throws SocketException {
setLabel(networkInterface.getDisplayName());
setMetaData("MTU", networkInterface.getMTU());
setMetaData("IsLoopback", networkInterface.isLoopback());
setMetaData("IsPointToPoint", networkInterface.isPointToPoint());
setMetaData("IsUp", networkInterface.isUp());
setMetaData("IsVirtual", networkInterface.isVirtual());
new BackgroundTask() {
@Override
public Object run() {
for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
try {
put(address.getAddress() + "/" + address.getNetworkPrefixLength(),
new SubnetMatrix(address));
} catch (SocketException e) {
e.printStackTrace();
}
}
return null;
}
};
}
项目:torgo
文件:NetUtils.java
/**
* Get an IP.
*
* @return the first IP address available.
* @throws SocketException on network error.
*/
public static InetAddress getIP() throws SocketException {
if (ip != null) {
return ip;
}
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
NetworkInterface ni;
while (nis.hasMoreElements()) {
ni = nis.nextElement();
if (!ni.isLoopback()/*not loopback*/ && ni.isUp()/*it works now*/) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
//filter for ipv4/ipv6
if (ia.getAddress().getAddress().length == 4) {
//4 for ipv4, 16 for ipv6
ip = ia.getAddress();
break;
}
}
}
}
return ip;
}
项目:torgo
文件:NetUtils.java
/**
* Get all IPs.
*
* @return all IP addresses available.
* @throws SocketException on network error.
*/
public static InetAddress[] getIPs() throws SocketException {
if (ips != null) {
return ips;
}
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
NetworkInterface ni;
List<InetAddress> ret = new ArrayList<>();
while (nis.hasMoreElements()) {
ni = nis.nextElement();
if (!ni.isLoopback()/*not loopback*/ && ni.isUp()/*it works now*/) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
//filter for ipv4/ipv6
if (ia.getAddress().getAddress().length == 4) {
//4 for ipv4, 16 for ipv6
ip = ia.getAddress();
ret.add(ip);
// return _ip;
}
}
}
}
ips = ret.toArray(new InetAddress[]{});
return ips;
}
项目:orvibo-sdk
文件:TransportManager.java
/**
* Gets the first active i pv4 interface.
*
* @return the first active i pv4 interface
* @throws SocketException the socket exception
*/
private NetworkInterface getFirstActiveIPv4Interface() throws SocketException {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface iface = networkInterfaces.nextElement();
if (iface.isUp() && !iface.isLoopback()) {
for (InterfaceAddress ifaceAddr : iface.getInterfaceAddresses()) {
if (ifaceAddr.getAddress() instanceof Inet4Address) {
return iface;
}
}
}
}
logger.debug("Unable to retrieve active network interface.");
return null;
}
项目:orvibo-sdk
文件:TransportManager.java
/**
* Checks if is local address.
*
* @param address the address
* @return true, if is local address
*/
private boolean isLocalAddress(InetAddress address) {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface iface = networkInterfaces.nextElement();
if (iface.isUp()) {
for (InterfaceAddress ifaceAddr : iface.getInterfaceAddresses()) {
if (ifaceAddr.getAddress().equals(address)) {
return true;
}
}
}
}
} catch (SocketException ex) {
logger.error("Error while determining if address is local");
}
return false;
}
项目:j2objc
文件:NetworkInterfaceTest.java
public void testLoopback() throws Exception {
// We know lo shouldn't have a hardware address.
NetworkInterface lo = NetworkInterface.getByName("lo0");
assertNull(lo.getHardwareAddress());
// But eth0, if it exists, should...
NetworkInterface eth0 = NetworkInterface.getByName("eth0");
if (eth0 != null) {
assertEquals(6, eth0.getHardwareAddress().length);
for (InterfaceAddress ia : eth0.getInterfaceAddresses()) {
if (ia.getAddress() instanceof Inet4Address) {
assertNotNull(ia.getBroadcast());
}
}
}
}
项目:zeppelin
文件:RemoteInterpreterUtils.java
public static String findAvailableHostAddress() throws UnknownHostException, SocketException {
InetAddress address = InetAddress.getLocalHost();
if (address.isLoopbackAddress()) {
for (NetworkInterface networkInterface : Collections
.list(NetworkInterface.getNetworkInterfaces())) {
if (!networkInterface.isLoopback()) {
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
InetAddress a = interfaceAddress.getAddress();
if (a instanceof Inet4Address) {
return a.getHostAddress();
}
}
}
}
}
return address.getHostAddress();
}
项目:cosc111
文件:GetNetInterfaceInfo.java
static void displayInterfaceInformation(NetworkInterface netIntf) throws SocketException {
System.out.printf("Display name: %s\n", netIntf.getDisplayName());
System.out.printf("Name: %s\n", netIntf.getName());
System.out.printf("Supports multicast: %s\n",netIntf.supportsMulticast());
System.out.printf("Is Up: %s\n",netIntf.isUp());
System.out.printf("Is Virtual: %s\n",netIntf.isVirtual());
System.out.printf("Is Loopback: %s\n",netIntf.isLoopback());
System.out.printf("MTU: %d\n",netIntf.getMTU());
List<InterfaceAddress> intfAddresses = netIntf.getInterfaceAddresses();
if(!intfAddresses.isEmpty()){
System.out.println("Interface Addresses:");
int i = 0;
for (InterfaceAddress intfAddress : intfAddresses) {
System.out.printf(" [%d]InetAddress: %s\n", i, intfAddress.getAddress());
System.out.printf(" Broadcast InetAddress: %s\n", intfAddress.getBroadcast());
i++;
}
System.out.printf("\n");
}
}
项目:jbossws-cxf
文件:UDPEndpointAPITestCase.java
private boolean isBroadcastAddressAvailable(NetworkInterface networkInterface) {
try
{
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses())
{
InetAddress broadcast = interfaceAddress.getBroadcast();
if (broadcast != null)
{
return true;
}
}
}
catch (Throwable e)
{
//in case networkInterface.getInterfaceAddresses() throws NPE on windows
}
return false;
}
项目:ForgePE
文件:MainActivity.java
public String[] getBroadcastAddresses() {
ArrayList<String> list = new ArrayList<>();
try {
System.setProperty("java.net.preferIPv4Stack", "true");
Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces();
while (niEnum.hasMoreElements()) {
NetworkInterface ni = niEnum.nextElement();
if (!ni.isLoopback()) {
for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
if (interfaceAddress.getBroadcast() != null)
list.add(interfaceAddress.getBroadcast().toString().substring(1));
}
}
}
} catch (Exception ignored) {
}
return list.toArray(new String[list.size()]);
}
项目:DroidDLNA
文件:NetworkAddressFactoryImpl.java
protected InetAddress getBindAddressInSubnetOf(InetAddress inetAddress) {
synchronized (networkInterfaces) {
for (NetworkInterface iface : networkInterfaces) {
for (InterfaceAddress ifaceAddress : getInterfaceAddresses(iface)) {
synchronized (bindAddresses) {
if (ifaceAddress == null || !bindAddresses.contains(ifaceAddress.getAddress())) {
continue;
}
}
if (isInSubnet(
inetAddress.getAddress(),
ifaceAddress.getAddress().getAddress(),
ifaceAddress.getNetworkPrefixLength())
) {
return ifaceAddress.getAddress();
}
}
}
}
return null;
}
项目:infobip-open-jdk-8
文件:NetworkPrefixLength.java
public static void main(String[] args) throws Exception {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
while (nics.hasMoreElements()) {
NetworkInterface nic = nics.nextElement();
for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
boolean valid = checkPrefix(iaddr);
if (!valid) {
passed = false;
debug(nic.getName(), iaddr);
}
InetAddress ia = iaddr.getAddress();
if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
// assumption: prefix length will always be 8
if (iaddr.getNetworkPrefixLength() != 8) {
out.println("Expected prefix of 8, got " + iaddr);
passed = false;
}
}
}
}
if (!passed)
throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
项目:jdk8u-dev-jdk
文件:NetworkPrefixLength.java
public static void main(String[] args) throws Exception {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
while (nics.hasMoreElements()) {
NetworkInterface nic = nics.nextElement();
for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
boolean valid = checkPrefix(iaddr);
if (!valid) {
passed = false;
debug(nic.getName(), iaddr);
}
InetAddress ia = iaddr.getAddress();
if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
// assumption: prefix length will always be 8
if (iaddr.getNetworkPrefixLength() != 8) {
out.println("Expected prefix of 8, got " + iaddr);
passed = false;
}
}
}
}
if (!passed)
throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
项目:torgo-android
文件:NetUtils.java
/**
* Get an IP.
*
* @return
* @throws SocketException
*/
public static InetAddress getIP() throws SocketException {
if (_ip != null) {
return _ip;
}
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
NetworkInterface ni;
while (nis.hasMoreElements()) {
ni = nis.nextElement();
if (!ni.isLoopback()/*not loopback*/ && ni.isUp()/*it works now*/) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
//filter for ipv4/ipv6
if (ia.getAddress().getAddress().length == 4) {
//4 for ipv4, 16 for ipv6
_ip = ia.getAddress();
return _ip;
}
}
}
}
return null;
}
项目:torgo-android
文件:NetUtils.java
/**
* Get all IPs.
*
* @return
* @throws SocketException
*/
public static InetAddress[] getIPs() throws SocketException {
if (_ips != null) {
return _ips;
}
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
NetworkInterface ni;
ArrayList<InetAddress> ret = new ArrayList<InetAddress>();
while (nis.hasMoreElements()) {
ni = nis.nextElement();
if (!ni.isLoopback()/*not loopback*/ && ni.isUp()/*it works now*/) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
//filter for ipv4/ipv6
if (ia.getAddress().getAddress().length == 4) {
//4 for ipv4, 16 for ipv6
_ip = ia.getAddress();
ret.add(_ip);
// return _ip;
}
}
}
}
_ips = ret.toArray(new InetAddress[]{});
return _ips;
}
项目:logstash
文件:NetworkUtils.java
private InetAddress getLocalAddress(String adaptorName){
try {
Enumeration<NetworkInterface> b = NetworkInterface.getNetworkInterfaces();
while (b.hasMoreElements()) {
NetworkInterface networkInterface = b.nextElement();
if (networkInterface.getName().equals(adaptorName)) {
for (InterfaceAddress f : networkInterface.getInterfaceAddresses()) {
if (f.getAddress().isSiteLocalAddress()) {
return f.getAddress();
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return null;
}
项目:AndroidLIFX
文件:TestCMD.java
private static InetAddress findBroadcastAddress() {
try {
ArrayList<NetworkInterface> ifaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface i : ifaces) {
if (!i.isLoopback()) {
List<InterfaceAddress> addresses = i.getInterfaceAddresses();
for (InterfaceAddress a : addresses) {
if (a.getAddress() instanceof Inet4Address) {
return a.getBroadcast();
}
}
}
}
} catch (SocketException e) {
Logger.log("unable to do network stuff.", e);
}
return null;
}
项目:jdk7-jdk
文件:NetworkPrefixLength.java
public static void main(String[] args) throws Exception {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
while (nics.hasMoreElements()) {
NetworkInterface nic = nics.nextElement();
for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
boolean valid = checkPrefix(iaddr);
if (!valid) {
passed = false;
debug(nic.getName(), iaddr);
}
}
}
if (!passed)
throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
项目:2FactorWallet
文件:IPAddressHelper.java
/**
* Returns a list of IP addresses that can be used to communicate with a peer on a
* different machine. The IP addresses are in CIDR notation (e.g. 192.168.2.1/24).
* @return
*/
public static List<String> getAllUsableIPAddresses(){
List<String> output = new ArrayList<String>();
try {
for (NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces())){
for(InterfaceAddress address : ni.getInterfaceAddresses()){
if(!address.getAddress().isMulticastAddress()
&& !address.getAddress().isLinkLocalAddress()
&& !address.getAddress().isLoopbackAddress()){
output.add(address.getAddress().getHostAddress() + "/" + address.getNetworkPrefixLength());
}
}
}
}catch(SocketException e){
}
return output;
}
项目:android-chat-telnet
文件:EnvoieMessage.java
static InetAddress ip() throws SocketException {
// on recupere toutes les interfaces
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
NetworkInterface ni;
while (nis.hasMoreElements()) {
ni = nis.nextElement();
if (!ni.isLoopback()/*pas la boucle locale*/ && ni.isUp()/*et active*/) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
//filtre ipv4/ipv6
if (ia.getAddress().getAddress().length == 4) {
//4 pour ipv4, 16 pour ipv6
return ia.getAddress();
}
}
}
}
return null;
}