Search in sources :

Example 86 with InetAddress

use of java.net.InetAddress in project flink by apache.

the class NetUtilsTest method testIPv6URLEncoding.

@Test
public void testIPv6URLEncoding() {
    try {
        final String addressString = "2001:db8:10:11:12:ff00:42:8329";
        final String bracketedAddressString = '[' + addressString + ']';
        final int port = 23453;
        InetAddress address = InetAddress.getByName(addressString);
        InetSocketAddress socketAddress = new InetSocketAddress(address, port);
        assertEquals(bracketedAddressString, NetUtils.ipAddressToUrlString(address));
        assertEquals(bracketedAddressString + ':' + port, NetUtils.ipAddressAndPortToUrlString(address, port));
        assertEquals(bracketedAddressString + ':' + port, NetUtils.socketAddressToUrlString(socketAddress));
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) UnknownHostException(java.net.UnknownHostException) Test(org.junit.Test)

Example 87 with InetAddress

use of java.net.InetAddress in project flink by apache.

the class SocketWindowWordCountITCase method testJavaProgram.

@Test
public void testJavaProgram() throws Exception {
    InetAddress localhost = InetAddress.getByName("localhost");
    // suppress sysout messages from this example
    final PrintStream originalSysout = System.out;
    final PrintStream originalSyserr = System.err;
    final ByteArrayOutputStream errorMessages = new ByteArrayOutputStream();
    System.setOut(new PrintStream(new NullStream()));
    System.setErr(new PrintStream(errorMessages));
    try {
        try (ServerSocket server = new ServerSocket(0, 10, localhost)) {
            final ServerThread serverThread = new ServerThread(server);
            serverThread.setDaemon(true);
            serverThread.start();
            final int serverPort = server.getLocalPort();
            org.apache.flink.streaming.examples.socket.SocketWindowWordCount.main(new String[] { "--port", String.valueOf(serverPort) });
            if (errorMessages.size() != 0) {
                fail("Found error message: " + new String(errorMessages.toByteArray(), ConfigConstants.DEFAULT_CHARSET));
            }
            serverThread.join();
            serverThread.checkError();
        }
    } finally {
        System.setOut(originalSysout);
        System.setErr(originalSyserr);
    }
}
Also used : PrintStream(java.io.PrintStream) ServerSocket(java.net.ServerSocket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 88 with InetAddress

use of java.net.InetAddress in project flink by apache.

the class SocketWindowWordCountITCase method testScalaProgram.

@Test
public void testScalaProgram() throws Exception {
    InetAddress localhost = InetAddress.getByName("localhost");
    // suppress sysout messages from this example
    final PrintStream originalSysout = System.out;
    final PrintStream originalSyserr = System.err;
    final ByteArrayOutputStream errorMessages = new ByteArrayOutputStream();
    System.setOut(new PrintStream(new NullStream()));
    System.setErr(new PrintStream(errorMessages));
    try {
        try (ServerSocket server = new ServerSocket(0, 10, localhost)) {
            final ServerThread serverThread = new ServerThread(server);
            serverThread.setDaemon(true);
            serverThread.start();
            final int serverPort = server.getLocalPort();
            org.apache.flink.streaming.scala.examples.socket.SocketWindowWordCount.main(new String[] { "--port", String.valueOf(serverPort) });
            if (errorMessages.size() != 0) {
                fail("Found error message: " + new String(errorMessages.toByteArray(), ConfigConstants.DEFAULT_CHARSET));
            }
            serverThread.join();
            serverThread.checkError();
        }
    } finally {
        System.setOut(originalSysout);
        System.setErr(originalSyserr);
    }
}
Also used : PrintStream(java.io.PrintStream) ServerSocket(java.net.ServerSocket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 89 with InetAddress

use of java.net.InetAddress in project blade by biezhi.

the class IPKit method getRealIp.

/**
	 * @return 本机IP
	 * @throws SocketException
	 */
public static String getRealIp() throws SocketException {
    // 本地IP,如果没有配置外网IP则返回它
    String localip = null;
    // 外网IP
    String netip = null;
    Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
    InetAddress ip = null;
    // 是否找到外网IP
    boolean finded = false;
    while (netInterfaces.hasMoreElements() && !finded) {
        NetworkInterface ni = netInterfaces.nextElement();
        Enumeration<InetAddress> address = ni.getInetAddresses();
        while (address.hasMoreElements()) {
            ip = address.nextElement();
            if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
                // 外网IP
                netip = ip.getHostAddress();
                finded = true;
                break;
            } else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
                // 内网IP
                localip = ip.getHostAddress();
            }
        }
    }
    if (netip != null && !"".equals(netip)) {
        return netip;
    } else {
        return localip;
    }
}
Also used : NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 90 with InetAddress

use of java.net.InetAddress in project hazelcast by hazelcast.

the class Address method resolve.

private static InetAddress resolve(InetSocketAddress inetSocketAddress) {
    checkNotNull(inetSocketAddress, "inetSocketAddress can't be null");
    InetAddress address = inetSocketAddress.getAddress();
    if (address == null) {
        throw new IllegalArgumentException("Can't resolve address: " + inetSocketAddress);
    }
    return address;
}
Also used : 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