📄 iputils.java
字号:
LOG.fine("Adding loopback interfaces"); } } if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("Returning " + allAddr.size() + " addresses."); } return allAddr.iterator(); } /** * Normalized version of {@link java.net.InetAddress#getHostAddress()} that * handles IPv6 addresss formatting using the style of IETF RFC 2732 and * also handles removal of IPv6 scoping identifiers. * * @see <a href="http://www.ietf.org/rfc/rfc2732.txt" target="_blank">IETF RFC 2732 <i>MIME : IPv6 Literal Addresses in URL's</i></a> * @param anAddress The address to format as a <tt>String</tt>. * @return The addresss formatted as a String. */ public static String getHostAddress(InetAddress anAddress) { String hostAddress; if (anAddress instanceof Inet6Address) { hostAddress = anAddress.getHostAddress(); int percentAt = hostAddress.indexOf('%'); if (-1 == percentAt) { // no scoping identifier. Just add the brackets. hostAddress = "[" + hostAddress + "]"; } else { // Remove scoping identifier. They aren't relevant when published. hostAddress = "[" + hostAddress.substring(0, percentAt) + "]"; } } else { hostAddress = anAddress.getHostAddress(); } return hostAddress; } /** * Parses a String containing a SokectAddress formatted as either: * <p/> * <pre> * <host> ":" <port> * * "[" <numeric_host> "]:" <port> * </pre> * <p/> * @param anAddress The address string to be parsed. * @return The parsed address. */ public static InetSocketAddress parseSocketAddress(String anAddress) { String hostAddress; String port; if (anAddress.startsWith("[")) { int endBracketAt = anAddress.indexOf(']'); int portSeparatorAt = anAddress.lastIndexOf(':'); if (-1 == endBracketAt) { throw new IllegalArgumentException("missing final ]"); } if (-1 == portSeparatorAt) { throw new IllegalArgumentException("missing port separator"); } if (portSeparatorAt < endBracketAt) { throw new IllegalArgumentException("missing port"); } hostAddress = anAddress.substring(1, endBracketAt); port = anAddress.substring(portSeparatorAt); } else { int portSeparatorAt = anAddress.lastIndexOf(':'); if (-1 == portSeparatorAt) { throw new IllegalArgumentException("missing port separator"); } hostAddress = anAddress.substring(0, portSeparatorAt); port = anAddress.substring(portSeparatorAt + 1); } int portNum = Integer.parseInt(port); return InetSocketAddress.createUnresolved(hostAddress, portNum); } /** * Create a client socket using the configured socketFactory or * connectToFromNoFactory if none is available. * * @param inetAddress Destination address * @param port Destination port * @param usingInterface Interface to use * @param localPort local port * @param timeout timeout in millis * @return a client socket with the JDK1.4 method connect(). * @throws IOException if an io error occurs */ public static Socket connectToFrom(InetAddress inetAddress, int port, InetAddress usingInterface, int localPort, int timeout) throws IOException { if (socketFactory != null) { return socketFactory.createConnection(inetAddress, port, usingInterface, localPort, timeout); } else { return connectToFromNoFactory(inetAddress, port, usingInterface, localPort, timeout); } } /** * Create a client socket with the JDK1.4 method connect(). * * @param inetAddress Destination address * @param port Destination port * @param usingInterface Interface to use * @param localPort local port * @param timeout timeout in millis * @return a client socket with the JDK1.4 method connect(). * @throws IOException if an io error occurs */ public static Socket connectToFromNoFactory(InetAddress inetAddress, int port, InetAddress usingInterface, int localPort, int timeout) throws IOException { Socket socket = new Socket(); InetSocketAddress src = new InetSocketAddress(usingInterface, localPort); InetSocketAddress dst = new InetSocketAddress(inetAddress, port); socket.bind(src); socket.connect(dst, timeout); return socket; } /** * makes connectToFrom create sockets with this factory. * * @param sf is the socket factory to use or null if you want the * default behaviour provided by connectToFromNoFactory(). */ public static void setSocketFactory(SocketFactory sf) { socketFactory = sf; } /** * returns the socketFactory used by connectToFrom() to create sockets, or * null if connectToFromNoFactory() is being used. * * @return the socket factory used by connectToFrom() or null if * the connectToFromNoFactory() method is used to create Sockets. */ public static SocketFactory getSocketFactory() { return socketFactory; } /** * makes connectToFrom create sockets with this factory. * * @param sf is the socket factory to use or null if you want the * default behaviour provided by new SeverSocket(). */ public static void setServerSocketFactory(ServerSocketFactory sf) { serverSocketFactory = sf; } /** * returns the ServerSocketFactory to create server sockets, or * null if new SeverSocket() is being used. * * @return the socket factory used or null if * the new SeverSocket() method is used to create ServerSockets. */ public static ServerSocketFactory getServerSocketFactory() { return serverSocketFactory; } /** * Size of port groups we will probe. */ final static int rangesize = 200; /** * Open a ServerSocket in the specified range. * * <p/> * The method used is done so that the entire range is examined if * needed while ensuring that the process eventually terminates if no port * is available. * * @param start The lowest numbered port to try. * @param end The highest numbered port to try. * @param backlog the allowed backlog of unaccepted connections. * @param bindAddress the InetAddress to which to bind. * @return a ServerSocket in the specified range. * @throws IOException when the socket cannot be opened. (Lame, but that's what ServerSocket says). */ public static ServerSocket openServerSocketInRange(int start, int end, int backlog, InetAddress bindAddress) throws IOException { ServerSocketFactory factory = getServerSocketFactory(); if ((start < 1) || (start > 65535)) { throw new IllegalArgumentException("Invalid start port"); } if ((end < 1) || (end > 65535) || (end < start)) { throw new IllegalArgumentException("Invalid end port"); } // fill the inRange array. List<Integer> inRange = new ArrayList<Integer>(rangesize); for (int eachInRange = 0; eachInRange < rangesize; eachInRange++) { inRange.add(eachInRange, eachInRange); } // fill the ranges array. List<Integer> ranges = new ArrayList<Integer>(); int starts = start; while (starts <= end) { ranges.add(starts); starts += rangesize; } // shuffle the ranges Collections.shuffle(ranges); while (!ranges.isEmpty()) { int range = ranges.remove(0); // reshuffle the inRange Collections.shuffle(inRange); for (int eachInRange = 0; eachInRange < rangesize; eachInRange++) { int tryPort = range + inRange.get(eachInRange); if (tryPort > end) { continue; } try { ServerSocket result; if (null == factory) { result = new ServerSocket(tryPort, backlog, bindAddress); } else { result = factory.createServerSocket(tryPort, backlog, bindAddress); } return result; } catch (BindException failed) {// this one is busy. try another. } } } throw new BindException("All ports in range are in use."); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -