📄 iputils.java
字号:
allAddr.add( anAddr ); } } catch ( Throwable caught ) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("Could not get addresses for " + anInterface, caught); } } } // if nothing suitable was found then return loopback address. if( allAddr.isEmpty() || Boolean.getBoolean("net.jxta.impl.IPUtils.localOnly")) { if( null != LOOPBACKV4 ) { allAddr.add( LOOPBACKV4 ); } if( null != LOOPBACKV6 ) { allAddr.add( LOOPBACKV6 ); } if (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug("Adding loopback interfaces"); } } if (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug("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. * * @param anAddress The address to format as a <tt>String</tt>. * @return The addresss formatted as a String. * * {@link <a href="http://www.ietf.org/rfc/rfc2732.txt" target="_blank">IETF RFC 2732 <i>MIME : IPv6 Literal Addresses in URL's</i></a>}. **/ 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; } /** * Create a client socket using the configured socketFactory or * connectToFromNoFactory if none is available. **/ 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(). **/ 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. * @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 inRange = new ArrayList(rangesize); for( int eachInRange = 0; eachInRange < rangesize; eachInRange++ ) { inRange.add( eachInRange, new Integer(eachInRange) ); } // fill the ranges array. List ranges = new ArrayList( ); int starts = start; while( starts <= end ) { ranges.add( new Integer( starts ) ); starts += rangesize; } // shuffle the ranges Collections.shuffle( ranges ); while( !ranges.isEmpty() ) { int range = ((Integer) ranges.remove(0)).intValue(); // reshuffle the inRange Collections.shuffle( inRange ); for( int eachInRange = 0; eachInRange < rangesize; eachInRange++ ) { int tryPort = range + ((Integer) inRange.get(eachInRange)).intValue(); 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 + -