directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Thomas Muller" <...@online.no>
Subject RE: Detecting LDAP Port busy at start-up (Repost)
Date Wed, 07 Jun 2006 09:34:18 GMT
Your observations about sockets, processes and bind address/port are correct
for server-sockets not using the SO_REUSEADDR option. Note that
implementations of SO_REUSEADDR differs between platforms, see e.g.
http://lists.spread.org/pipermail/spread-users/2001-September/000354.html

In general two processes/threads can't open a server-socket that bind to the
same interface and port - note that for the loopback interface you can have
several server sockets listening to the same port, but you have to use
different localhost addresses (127.0.0.x). 

Programmatically (Java) you can it like this:

	public static boolean isAvailable( int port ) {
		return isAvailable( "localhost", port );
	}

	public static boolean isAvailable( String localhost, int port ) {
		Require.notNull( localhost );

		String localHostName = null;

		try {
			localHostName =
InetAddress.getLocalHost().getHostName();
		} catch( IOException e ) {
			return false;
		}

		Require.that( localhost.equals( localHostName )
				|| localhost.toLowerCase().equals(
"localhost" )
				|| localhost.equals( "0.0.0.0" )
				|| localhost.startsWith( "127" ) );
		Require.that( 0 < port && port <= 0xFFFF );

		boolean result = false;

		ServerSocket ss = null;

		try {
			ss = new ServerSocket();

			InetSocketAddress address = new InetSocketAddress(
					localhost, port );

			log.debug( "isAvailable() : binding server socket to
[" + address
					+ "]" );

			ss.bind( address );

			result = true;

		} catch( IOException e ) {
			log.debug( "Failed to bind [" + localhost + ":" +
port + "]" );

		} finally {
			if( ss != null ) {
				try {
					ss.close();
				} catch( IOException ignore ) { }
			}
		}

		log.debug( "isAvailable [" + localhost + ":" + port + "]=["
				+ result + "]" );

		return result;
	}

>From the OS you can run netstat -p TCP | grep <port> and check whether you
get line that ends with LISTENING or TIME_WAIT.

Hope this helps,

--

Thomas




|  -----Original Message-----
|  From: Norbet Reilly [mailto:nrhope@gmail.com] 
|  Sent: 07 June 2006 06:09
|  To: Apache Directory Developers List
|  Subject: Re: Detecting LDAP Port busy at start-up (Repost)
|  
|  The usecase I have in mind is trying to detect if there is already an
|  OpenLDAP directory (C/C++) listening to the same port as A-DS, at the
|  time that A-DS is started.
|  
|  In this situtation I have found that the OpenLDAP directory gets all
|  the traffic but there is no way to draw the user's attention to the
|  problem.
|  
|  My understanding is that ServerSocket.bind() would detect the
|  situation, but because of the current implementation (based on nio)
|  the check is simply not being performed. I had in mind the Tomcat
|  behaviour, where it won't start up when it's port is busy (or is it
|  just the easier case of simply detecting when another Tomcat is using
|  the same port ?). I thought there was stuff in socket support to tell
|  if a particular socket was already being listened to, but then it's
|  been a long time since I last visited this low in the comms stack...
|  
|  Quoting from http://www.cafeaulait.org/course/week12/26.html  (which
|  is admittedly old) which I found in a google :
|  "When you create a ServerSocket object, it attempts to bind to the
|  port on the local host given by the port argument. If another server
|  socket is already listening to the port, then a
|  java.net.BindException, a subclass of java.io.IOException, is thrown.
|  No more than one process or thread can listen to a particular port at
|  a time. This includes non-Java processes or threads. For example, if
|  there's already an HTTP server running on port 80, you won't be able
|  to bind to port 80."
|  
|  The rest of my post was thinking out load about how to allow flexible
|  configuration of the "port busy" check, assuming that this 
|  check would
|  not required in all deployment situations (although it seems to me to
|  be the more sensible default).
|  
|  
|  


Mime
View raw message