private Channel bindToPrivilegedPort ( ) throws RpcException { System . out . println ( "Attempting to use privileged port." ) ; for ( int port = 1023 ; port > 0 ; -- port ) { try { ChannelPipeline pipeline = _clientBootstrap . getPipelineFactory ( ) . getPipeline ( ) ; Channel channel = _clientBootstrap . getFactory ( ) . newChannel ( pipeline ) ; channel . getConfig ( ) . setOptions ( _clientBootstrap . getOptions ( ) ) ; ChannelFuture bindFuture = channel . bind ( new InetSocketAddress ( port ) ) . awaitUninterruptibly ( ) ; if ( bindFuture . isSuccess ( ) ) { System . out . println ( "Success! Bound to port " + port ) ; return bindFuture . getChannel ( ) ; } } catch ( Exception e ) { String msg = String . format ( "rpc request bind error for address: %s" , getRemoteAddress ( ) ) ; throw new RpcException ( RpcStatus . NETWORK_ERROR , msg , e ) ; } } throw new RpcException ( RpcStatus . LOCAL_BINDING_ERROR , String . format ( "Cannot bind a port < 1024: %s" , getRemoteAddress ( ) ) ) ; }
This attempts to bind to privileged ports starting with 1023 and working downwards and returns when the first binding succeeds .