JXTA

net.jxta.impl.endpoint
Class IPUtils

java.lang.Object
  extended by net.jxta.impl.endpoint.IPUtils

public final class IPUtils
extends Object

Utility methods for use by IP based transports.


Field Summary
static InetAddress ANYADDRESS
          Constant which works as the IP "Any Address" value
static InetAddress ANYADDRESSV4
           
static InetAddress ANYADDRESSV6
           
(package private) static String IPV4ANYADDRESS
           
(package private) static String IPV4LOOPBACK
           
(package private) static String IPV6ANYADDRESS
           
(package private) static String IPV6LOOPBACK
           
static InetAddress LOOPBACK
          Constant which works as the IP "Local Loopback" value;
static InetAddress LOOPBACKV4
           
static InetAddress LOOPBACKV6
           
(package private) static Random random
           
(package private) static int rangesize
          Size of port groups we will probe.
 
Method Summary
static Socket connectToFrom(InetAddress inetAddress, int port, InetAddress usingInterface, int localPort, int timeout)
          Create a client socket using the configured socketFactory or connectToFromNoFactory if none is available.
static Socket connectToFromNoFactory(InetAddress inetAddress, int port, InetAddress usingInterface, int localPort, int timeout)
          Create a client socket with the JDK1.4 method connect().
static Iterator<InetAddress> getAllLocalAddresses()
          Provide an iterator which returns all of the local InetAddresses for this host.
static String getHostAddress(InetAddress anAddress)
          Normalized version of InetAddress.getHostAddress() that handles IPv6 addresss formatting using the style of IETF RFC 2732 and also handles removal of IPv6 scoping identifiers.
static ServerSocketFactory getServerSocketFactory()
          returns the ServerSocketFactory to create server sockets, or null if new SeverSocket() is being used.
static SocketFactory getSocketFactory()
          returns the socketFactory used by connectToFrom() to create sockets, or null if connectToFromNoFactory() is being used.
static ServerSocket openServerSocketInRange(int start, int end, int backlog, InetAddress bindAddress)
          Open a ServerSocket in the specified range.
static InetSocketAddress parseSocketAddress(String anAddress)
          Parses a String containing a SokectAddress formatted as either:

<host> ":" <port> "[" <numeric_host> "]:" <port>

static void setServerSocketFactory(ServerSocketFactory sf)
          makes connectToFrom create sockets with this factory.
static void setSocketFactory(SocketFactory sf)
          makes connectToFrom create sockets with this factory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

random

static final Random random

IPV4ANYADDRESS

static final String IPV4ANYADDRESS
See Also:
Constant Field Values

IPV6ANYADDRESS

static final String IPV6ANYADDRESS
See Also:
Constant Field Values

IPV4LOOPBACK

static final String IPV4LOOPBACK
See Also:
Constant Field Values

IPV6LOOPBACK

static final String IPV6LOOPBACK
See Also:
Constant Field Values

ANYADDRESS

public static final InetAddress ANYADDRESS
Constant which works as the IP "Any Address" value


ANYADDRESSV4

public static final InetAddress ANYADDRESSV4

ANYADDRESSV6

public static final InetAddress ANYADDRESSV6

LOOPBACK

public static final InetAddress LOOPBACK
Constant which works as the IP "Local Loopback" value;


LOOPBACKV4

public static final InetAddress LOOPBACKV4

LOOPBACKV6

public static final InetAddress LOOPBACKV6

rangesize

static final int rangesize
Size of port groups we will probe.

See Also:
Constant Field Values
Method Detail

getAllLocalAddresses

public static Iterator<InetAddress> getAllLocalAddresses()
Provide an iterator which returns all of the local InetAddresses for this host.

Returns:
iterator of InetAddress which is all of the InetAddress for all local interfaces.

getHostAddress

public static String getHostAddress(InetAddress anAddress)
Normalized version of InetAddress.getHostAddress() that handles IPv6 addresss formatting using the style of IETF RFC 2732 and also handles removal of IPv6 scoping identifiers.

Parameters:
anAddress - The address to format as a String.
Returns:
The addresss formatted as a String.
See Also:
IETF RFC 2732 MIME : IPv6 Literal Addresses in URL's

parseSocketAddress

public static InetSocketAddress parseSocketAddress(String anAddress)
Parses a String containing a SokectAddress formatted as either:

     <host> ":" <port>

     "[" <numeric_host> "]:" <port>
  

Parameters:
anAddress - The address string to be parsed.
Returns:
The parsed address.

connectToFrom

public static Socket connectToFrom(InetAddress inetAddress,
                                   int port,
                                   InetAddress usingInterface,
                                   int localPort,
                                   int timeout)
                            throws IOException
Create a client socket using the configured socketFactory or connectToFromNoFactory if none is available.

Parameters:
inetAddress - Destination address
port - Destination port
usingInterface - Interface to use
localPort - local port
timeout - timeout in millis
Returns:
a client socket with the JDK1.4 method connect().
Throws:
IOException - if an io error occurs

connectToFromNoFactory

public static Socket connectToFromNoFactory(InetAddress inetAddress,
                                            int port,
                                            InetAddress usingInterface,
                                            int localPort,
                                            int timeout)
                                     throws IOException
Create a client socket with the JDK1.4 method connect().

Parameters:
inetAddress - Destination address
port - Destination port
usingInterface - Interface to use
localPort - local port
timeout - timeout in millis
Returns:
a client socket with the JDK1.4 method connect().
Throws:
IOException - if an io error occurs

setSocketFactory

public static void setSocketFactory(SocketFactory sf)
makes connectToFrom create sockets with this factory.

Parameters:
sf - is the socket factory to use or null if you want the default behaviour provided by connectToFromNoFactory().

getSocketFactory

public static SocketFactory getSocketFactory()
returns the socketFactory used by connectToFrom() to create sockets, or null if connectToFromNoFactory() is being used.

Returns:
the socket factory used by connectToFrom() or null if the connectToFromNoFactory() method is used to create Sockets.

setServerSocketFactory

public static void setServerSocketFactory(ServerSocketFactory sf)
makes connectToFrom create sockets with this factory.

Parameters:
sf - is the socket factory to use or null if you want the default behaviour provided by new SeverSocket().

getServerSocketFactory

public static ServerSocketFactory getServerSocketFactory()
returns the ServerSocketFactory to create server sockets, or null if new SeverSocket() is being used.

Returns:
the socket factory used or null if the new SeverSocket() method is used to create ServerSockets.

openServerSocketInRange

public static ServerSocket openServerSocketInRange(int start,
                                                   int end,
                                                   int backlog,
                                                   InetAddress bindAddress)
                                            throws IOException
Open a ServerSocket in the specified range.

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.

Parameters:
start - The lowest numbered port to try.
end - The highest numbered port to try.
backlog - the allowed backlog of unaccepted connections.
bindAddress - the InetAddress to which to bind.
Returns:
a ServerSocket in the specified range.
Throws:
IOException - when the socket cannot be opened. (Lame, but that's what ServerSocket says).

JXSE