List of utility methods to do InetAddress
URL | createAbsoluteURL(InetAddress address, int localStreamPort, URI relativeOrNot) create Absolute URL try { if (address instanceof Inet6Address) { return createAbsoluteURL(new URL("http://[" + address.getHostAddress() + "]:" + localStreamPort), relativeOrNot); } else if (address instanceof Inet4Address) { return createAbsoluteURL(new URL("http://" + address.getHostAddress() + ":" + localStreamPort), relativeOrNot); } else { ... |
DatagramSocket | createDatagramSocket(InetAddress addr, int port) Creates a DatagramSocket bound to addr. DatagramSocket sock = null; if (addr == null) { if (port == 0) { return new DatagramSocket(); } else { while (port < MAX_PORT) { try { return new DatagramSocket(port); ... |
InetSocketAddress | createInetSocketAddress(final InetAddress address) create Inet Socket Address if (address.isLoopbackAddress()) { return new InetSocketAddress(address, LOCAL_CLIENT_PORT); return new InetSocketAddress(address, CLIENT_PORT); |
InetSocketAddress | createResolved(InetAddress address, int port) Creates and returns a resolved InetSocketAddress . return new InetSocketAddress(address, port); |
DatagramSocket | createSocket(InetAddress address, int port, boolean fixedPort) Opens a socket on a specific network interface, either on a fixed or a random port. DatagramSocket result = null; if (fixedPort) { try { result = new DatagramSocket(new InetSocketAddress(address, port)); result.setSoTimeout(50); } catch (Exception e) { return null; } else { Random rdm = new Random(); port = port + rdm.nextInt(1024); while (result == null && port < 65530) { try { result = new DatagramSocket(new InetSocketAddress(address, port)); result.setSoTimeout(50); } catch (Exception e) { result = null; port++; return result; |
boolean | doMatch(InetAddress inetAddress, String toMatchParam) Checks a string macthes with a IP address. String hostAddres = inetAddress.getHostAddress(); if (toMatchParam.contains("-")) { if (hostAddres.contains(":")) { return false; String[] octects = toMatchParam.split("\\."); String[] octectsIA = hostAddres.split("\\."); String first3 = octects[0] + "." + octects[1] + "." + octects[2]; ... |
int | findFreePort(InetAddress address, int start, int end) Find free port in range for the specified IP address ServerSocket socket = null; try { for (int port = start; port <= end; ++port) { try { socket = new ServerSocket(port, 0, address); socket.setReuseAddress(true); return socket.getLocalPort(); } catch (IOException ignore) { ... |
int | findUnusedPort(InetAddress address, int from, int to) find Unused Port for (int i = 0; i < 12; i++) { ServerSocket ss = null; int port = getRandomPort(from, to); try { ss = new ServerSocket(); SocketAddress sa = new InetSocketAddress(address, port); ss.bind(sa); return ss.getLocalPort(); ... |
int | findUnusedPort(InetAddress address, int low, int high) Finds an unused local port between the given from and to values. if (high < low) return -1; for (int i = 0; i < 10; i++) { int port = getRandomPort(low, high); if (!isPortInUse(address, port)) return port; return -1; ... |
Scriptable | formatAddress(InetAddress address, int port, Context cx, Scriptable scope) format Address Scriptable addr = cx.newObject(scope); addr.put("port", addr, port); addr.put("address", addr, address.getHostAddress()); if (address instanceof Inet6Address) { addr.put("family", addr, "IPv6"); } else { addr.put("family", addr, "IPv4"); return addr; |