Search in sources :

Example 76 with InetAddress

use of java.net.InetAddress in project j2objc by google.

the class InetSocketAddressTest method test_ConstructorLInetAddressI.

public void test_ConstructorLInetAddressI() throws Exception {
    String[] validIPAddresses = { "::1.2.3.4", "::", "::", "1::0", "1::", "::1", "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255", "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0", "127.0.0.1", "localhost", "42.42.42.42", "0.0.0.0" };
    String[] results = { "0:0:0:0:0:0:102:304", "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0:0", "1:0:0:0:0:0:0:0", "1:0:0:0:0:0:0:0", "0:0:0:0:0:0:0:1", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0:0", "localhost", "localhost", "42.42.42.42", "0.0.0.0" };
    for (int i = 0; i < validIPAddresses.length; i++) {
        InetAddress ia = InetAddress.getByName(validIPAddresses[i]);
        InetSocketAddress isa = new InetSocketAddress(ia, 80);
        assertEquals(80, isa.getPort());
    //assertEquals(results[i], isa.getHostName());
    }
    InetSocketAddress isa = new InetSocketAddress((InetAddress) null, 80);
    assertEquals("0.0.0.0", isa.getHostName());
    try {
        new InetSocketAddress(InetAddress.getByName("localhost"), 65536);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
        new InetSocketAddress(InetAddress.getByName("localhost"), -1);
        fail();
    } catch (IllegalArgumentException expected) {
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Example 77 with InetAddress

use of java.net.InetAddress in project j2objc by google.

the class InetSocketAddressTest method test_getAddress.

public void test_getAddress() throws Exception {
    String[] validIPAddresses = { "::1.2.3.4", "::", "::", "1::0", "1::", "::1", "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255", "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0", "127.0.0.1", "localhost", "42.42.42.42", "0.0.0.0" };
    for (int i = 0; i < validIPAddresses.length; i++) {
        InetAddress ia = InetAddress.getByName(validIPAddresses[i]);
        InetSocketAddress isa = new InetSocketAddress(ia, 0);
        assertEquals(ia, isa.getAddress());
    }
    InetSocketAddress isa = new InetSocketAddress((InetAddress) null, 0);
    assertNotNull(isa.getAddress());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Example 78 with InetAddress

use of java.net.InetAddress in project j2objc by google.

the class OldAuthenticatorTest method test_setDefault.

public void test_setDefault() throws UnknownHostException {
    InetAddress addr = InetAddress.getLocalHost();
    PasswordAuthentication pa = Authenticator.requestPasswordAuthentication(addr, 8080, "http", "promt", "HTTP");
    assertNull(pa);
    MockAuthenticator mock = new MockAuthenticator();
    Authenticator.setDefault(mock);
    addr = InetAddress.getLocalHost();
    pa = Authenticator.requestPasswordAuthentication(addr, 80, "http", "promt", "HTTP");
    assertNull(pa);
    Authenticator.setDefault(null);
}
Also used : InetAddress(java.net.InetAddress) PasswordAuthentication(java.net.PasswordAuthentication)

Example 79 with InetAddress

use of java.net.InetAddress in project springside4 by springside.

the class NetUtil method initLocalAddress.

/**
	 * 初始化本地地址
	 */
private static void initLocalAddress() {
    NetworkInterface nic = null;
    // 根据命令行执行hostname获得本机hostname, 与/etc/hosts 中该hostname的第一条ip配置,获得ip地址
    try {
        localAddress = InetAddress.getLocalHost();
        nic = NetworkInterface.getByInetAddress(localAddress);
    } catch (Exception ignored) {
    // NOSONAR
    }
    // 如果结果为空,或是一个loopback地址(127.0.0.1), 或是ipv6地址,再遍历网卡尝试获取
    if (localAddress == null || nic == null || localAddress.isLoopbackAddress() || localAddress instanceof Inet6Address) {
        InetAddress lookedUpAddr = findLocalAddressViaNetworkInterface();
        // 仍然不符合要求,只好使用127.0.0.1
        try {
            localAddress = lookedUpAddr != null ? lookedUpAddr : InetAddress.getByName("127.0.0.1");
        } catch (UnknownHostException ignored) {
        // NOSONAR
        }
    }
    localHost = IPUtil.toString(localAddress);
    logger.info("localhost is {}", localHost);
}
Also used : UnknownHostException(java.net.UnknownHostException) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException)

Example 80 with InetAddress

use of java.net.InetAddress in project springside4 by springside.

the class NetUtil method findLocalAddressViaNetworkInterface.

/**
	 * 根据preferNamePrefix 与 defaultNicList的配置网卡,找出合适的网卡
	 */
private static InetAddress findLocalAddressViaNetworkInterface() {
    // 如果hostname +/etc/hosts 得到的是127.0.0.1, 则首选这块网卡
    String preferNamePrefix = SystemPropertiesUtil.getString("localhost.prefer.nic.prefix", "LOCALHOST_PREFER_NIC_PREFIX", "bond0.");
    // 如果hostname +/etc/hosts 得到的是127.0.0.1, 和首选网卡都不符合要求,则按顺序遍历下面的网卡
    String defaultNicList = SystemPropertiesUtil.getString("localhost.default.nic.list", "LOCALHOST_DEFAULT_NIC_LIST", "bond0,eth0,em0,br0");
    InetAddress resultAddress = null;
    Map<String, NetworkInterface> candidateInterfaces = MapUtil.newHashMap();
    // 遍历所有网卡,找出所有可用网卡,尝试找出符合prefer前缀的网卡
    try {
        for (Enumeration<NetworkInterface> allInterfaces = NetworkInterface.getNetworkInterfaces(); allInterfaces.hasMoreElements(); ) {
            NetworkInterface nic = allInterfaces.nextElement();
            // 检查网卡可用并支持广播
            try {
                if (!nic.isUp() || !nic.supportsMulticast()) {
                    continue;
                }
            } catch (SocketException e) {
                continue;
            }
            // 检查是否符合prefer前缀
            String name = nic.getName();
            if (name.startsWith(preferNamePrefix)) {
                // 检查有否非ipv6 非127.0.0.1的inetaddress
                resultAddress = findAvailableInetAddress(nic);
                if (resultAddress != null) {
                    return resultAddress;
                }
            } else {
                // 不是Prefer前缀,先放入可选列表
                candidateInterfaces.put(name, nic);
            }
        }
        for (String nifName : defaultNicList.split(",")) {
            NetworkInterface nic = candidateInterfaces.get(nifName);
            if (nic != null) {
                resultAddress = findAvailableInetAddress(nic);
                if (resultAddress != null) {
                    return resultAddress;
                }
            }
        }
    } catch (SocketException e) {
        return null;
    }
    return null;
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Aggregations

InetAddress (java.net.InetAddress)2225 UnknownHostException (java.net.UnknownHostException)423 IOException (java.io.IOException)295 Test (org.junit.Test)289 InetSocketAddress (java.net.InetSocketAddress)251 NetworkInterface (java.net.NetworkInterface)192 ArrayList (java.util.ArrayList)174 SocketException (java.net.SocketException)154 HashMap (java.util.HashMap)104 Inet6Address (java.net.Inet6Address)103 Inet4Address (java.net.Inet4Address)96 Socket (java.net.Socket)78 DatagramPacket (java.net.DatagramPacket)75 LinkAddress (android.net.LinkAddress)70 DatagramSocket (java.net.DatagramSocket)67 Token (org.apache.cassandra.dht.Token)67 Map (java.util.Map)59 RouteInfo (android.net.RouteInfo)56 LinkProperties (android.net.LinkProperties)52 ServerSocket (java.net.ServerSocket)52