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());
}
}
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);
}
}
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);
}
}
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;
}
}
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;
}
Aggregations