Code example for ServerSocket

Methods: bindcloseisBoundsetSoTimeout

0
ServerSocket s = null;		
		 
	   try  
	   { 
			s = new ServerSocket();
			// s.setReuseAddress(true);  -- NB: do NOT set - otherwise will "hijack"/ reuse socket already in use 
			s.setSoTimeout( 1 ); // 1ms block on accept call 
		    s.bind( new InetSocketAddress( "127.0.0.1", port ) );  		    
		    s.close();
		  
		    __log.debug( "port " + port + " isBound? " + s.isBound() );
		     
		    __log.info( "free port @ " + port );
		     
		    return true; 
		} 
		catch( IOException ex ) 
		{            
			 __log.info( "non-free port @ " + port + "; reason: " + ex.toString() );
		     return false; 
		} 
		finally 
		{ 
			try 
			{  
				if( s != null ) {
					s.close();
				} 
			} 
			catch( Exception ex ){ }
				 
			s = null;
Experience pair programming with AI  Get Codota for Java