faa.tg.net
Class Port

java.lang.Object
  extended by faa.tg.net.Port
All Implemented Interfaces:
java.lang.Comparable<Port>

public class Port
extends java.lang.Object
implements java.lang.Comparable<Port>

 The Port Class is utilized for specifying a TCP/UDP port number.
 
 Ports are consistent with equals and can be used as keys in
 hashTables.
 
 Ports can be used to create sockets of various types bound
 to the port that called them. These methods should have 
 annotations that link to the particular constructor used.
 
See the source: Port.java

Version:
$Id: Port.java,v 3.7 2009/01/26 17:15:15 lykensj Exp $
Author:
Dan Warburton, FAA/ACT-510
See Also:

Field Summary
static java.lang.String CVS_VERSION_ID
          The CVS Version ID
private  int port
          The TCP/UDP port number
static Port ZERO
          A zero port; has special meanings for certain sockets.
 
Constructor Summary
Port(int port)
          Construct a Port given an int
Port(java.lang.String portString)
          Construct a Port given a String.
 
Method Summary
 Port add(PortOffset offset)
          Creates a new port with the integer value being the sum of this port plus the offset given.
 int compareTo(Port o)
           
 java.net.DatagramSocket createDatagramSocket()
          Constructs a datagram socket and binds it to this port on the local host machine.
 java.net.DatagramSocket createDatagramSocket(java.net.InetAddress address)
          Creates a datagram socket, bound to the specified local address and this port.
 inria.net.lrmp.Lrmp createLrmp(java.lang.String address, int ttl, inria.net.lrmp.LrmpProfile profile)
           
 java.net.MulticastSocket createMulticastSocket()
          Create a multicast socket and bind it to this port.
 java.net.MulticastSocket createMulticastSocket(java.net.InetAddress address)
          Create a MulticastSocket bound to the specified socket address, using this port.
 java.net.ServerSocket createServerSocket()
          Creates a server socket, bound to this port.
 java.net.Socket createSocket(Host host)
          Creates a stream socket and connects it to this port at the specified host.
 java.net.Socket createSocket(java.net.InetAddress address)
          Creates a stream socket and connects it to this port at the specified IP address.
 java.net.Socket createSocket(java.net.InetAddress address, java.net.InetAddress localAddr, Port localPort)
          Creates a socket and connects it to the specified remote address on this remote port.
 boolean equals(java.lang.Object o)
           
 PortOffset getOffset()
          Generates an offset based on the last two decimal digits of this port.
 int hashCode()
           
 int intValue()
          Deprecated.  
 java.net.DatagramPacket makeDatagramPacket(byte[] data, java.net.InetAddress address)
          Constructs a datagram packet for sending packets of length data.length to this port number on the specified host.
 PortOffset offsetFrom(Port port)
          Generates an offset based on the difference of this to the the argument.
 java.lang.Integer toInteger()
           
 java.lang.String toString()
           
(package private)  int value()
          Accessor for the classes in this package.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

CVS_VERSION_ID

public static final java.lang.String CVS_VERSION_ID
The CVS Version ID

See Also:
Constant Field Values

ZERO

public static final Port ZERO
A zero port; has special meanings for certain sockets.


port

private final int port
The TCP/UDP port number

Constructor Detail

Port

public Port(int port)
Construct a Port given an int

Throws:
java.lang.NumberFormatException - if the argument is outside the range of an unsigned short [0,65535]

Port

public Port(java.lang.String portString)
Construct a Port given a String.

Throws:
java.lang.NumberFormatException - if the string does not contain a parsable integer, or if the parsed integer is outside the range of an unsigned short [0,65535]
Method Detail

intValue

public int intValue()
Deprecated. 

Deprecated since the underlying value shouldn't be accessed, but the various methods that do things should be called instead.

Returns:
the underlying value.

value

int value()
Accessor for the classes in this package. Use with care.

Returns:
the underlying value

toInteger

public java.lang.Integer toInteger()
Returns:
the underlying value

createLrmp

public inria.net.lrmp.Lrmp createLrmp(java.lang.String address,
                                      int ttl,
                                      inria.net.lrmp.LrmpProfile profile)
                               throws inria.net.lrmp.LrmpException
Throws:
inria.net.lrmp.LrmpException

makeDatagramPacket

public java.net.DatagramPacket makeDatagramPacket(byte[] data,
                                                  java.net.InetAddress address)
Constructs a datagram packet for sending packets of length data.length to this port number on the specified host. Data is not copied; a change in the byte array will be reflected in the packet.

Parameters:
data - the packet data.
See Also:
InetAddress, DatagramPacket.DatagramPacket(byte[], int, InetAddress, int)

createSocket

public java.net.Socket createSocket(java.net.InetAddress address)
                             throws java.io.IOException
Creates a stream socket and connects it to this port at the specified IP address.

If the application has specified a socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Parameters:
address - the IP address.
Throws:
java.io.IOException - if an I/O error occurs when creating the socket.
java.lang.SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
See Also:
Socket.setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, SocketImplFactory.createSocketImpl(), SecurityManager.checkConnect(java.lang.String, int), Socket.Socket(InetAddress, int)

createSocket

public java.net.Socket createSocket(java.net.InetAddress address,
                                    java.net.InetAddress localAddr,
                                    Port localPort)
                             throws java.io.IOException
Creates a socket and connects it to the specified remote address on this remote port. The Socket will also bind() to the local address and port supplied.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Parameters:
address - the remote address
localAddr - the local address the socket is bound to
localPort - the local port the socket is bound to
Throws:
java.io.IOException - if an I/O error occurs when creating the socket.
java.lang.SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
See Also:
SecurityManager.checkConnect(java.lang.String, int)

createServerSocket

public java.net.ServerSocket createServerSocket()
                                         throws java.io.IOException
Creates a server socket, bound to this port. A port of 0 creates a socket on any free port.

The maximum queue length for incoming connection indications (a request to connect) is set to 50. If a connection indication arrives when the queue is full, the connection is refused.

If the application has specified a server socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created.

If there is a security manager, its checkListen method is called with the port argument as its argument to ensure the operation is allowed. This could result in a SecurityException.

Throws:
java.io.IOException - if an I/O error occurs when opening the socket.
java.lang.SecurityException - if a security manager exists and its checkListen method doesn't allow the operation.
See Also:
SocketImpl, SocketImplFactory.createSocketImpl(), ServerSocket.setSocketFactory(java.net.SocketImplFactory), SecurityManager.checkListen(int), ServerSocket.ServerSocket(int)

createMulticastSocket

public java.net.MulticastSocket createMulticastSocket()
                                               throws java.io.IOException
Create a multicast socket and bind it to this port.

If there is a security manager, its checkListen method is first called with this port as its argument to ensure the operation is allowed. This could result in a SecurityException.

When the socket is created the DatagramSocket.setReuseAddress(boolean) method is called to enable the SO_REUSEADDR socket option.

Throws:
java.io.IOException - if an I/O exception occurs while creating the MulticastSocket
java.lang.SecurityException - if a security manager exists and its checkListen method doesn't allow the operation.
See Also:
SecurityManager.checkListen(int), DatagramSocket.setReuseAddress(boolean), MulticastSocket.MulticastSocket(int)

createMulticastSocket

public java.net.MulticastSocket createMulticastSocket(java.net.InetAddress address)
                                               throws java.io.IOException
Create a MulticastSocket bound to the specified socket address, using this port.

Or, if the address is null, create an unbound socket.

If there is a security manager, its checkListen method is first called with this port as its argument to ensure the operation is allowed. This could result in a SecurityException.

When the socket is created the DatagramSocket.setReuseAddress(boolean) method is called to enable the SO_REUSEADDR socket option.

Parameters:
address - address to bind to, or null for an unbound socket.
Throws:
java.io.IOException - if an I/O exception occurs while creating the MulticastSocket
java.lang.SecurityException - if a security manager exists and its checkListen method doesn't allow the operation.
See Also:
SecurityManager.checkListen(int), DatagramSocket.setReuseAddress(boolean), MulticastSocket.MulticastSocket(java.net.SocketAddress)

createDatagramSocket

public java.net.DatagramSocket createDatagramSocket(java.net.InetAddress address)
                                             throws java.net.SocketException
Creates a datagram socket, bound to the specified local address and this port. If the IP address is 0.0.0.0, the socket will be bound to the wildcard address, an IP address chosen by the kernel.

If there is a security manager, its checkListen method is first called with the port argument as its argument to ensure the operation is allowed. This could result in a SecurityException.

Parameters:
laddr - local address to bind
Throws:
java.net.SocketException - if the socket could not be opened, or the socket could not bind to the specified local port.
java.lang.SecurityException - if a security manager exists and its checkListen method doesn't allow the operation.
See Also:
SecurityManager.checkListen(int), DatagramSocket.DatagramSocket(int, InetAddress)

createDatagramSocket

public java.net.DatagramSocket createDatagramSocket()
                                             throws java.net.SocketException
Constructs a datagram socket and binds it to this port on the local host machine. The socket will be bound to the wildcard address, an IP address chosen by the kernel.

If there is a security manager, its checkListen method is first called with the port argument as its argument to ensure the operation is allowed. This could result in a SecurityException.

Throws:
java.net.SocketException - if the socket could not be opened, or the socket could not bind to the specified local port.
java.lang.SecurityException - if a security manager exists and its checkListen method doesn't allow the operation.
See Also:
SecurityManager.checkListen(int), DatagramSocket.DatagramSocket(int)

createSocket

public java.net.Socket createSocket(Host host)
                             throws java.io.IOException
Creates a stream socket and connects it to this port at the specified host.

If the application has specified a socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created.

If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.

Parameters:
host - effectively, the IP address.
Throws:
java.io.IOException - if an I/O error occurs when creating the socket.
java.lang.SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
See Also:
Socket.setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, SocketImplFactory.createSocketImpl(), SecurityManager.checkConnect(java.lang.String, int), Socket.Socket(InetAddress, int)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

add

public Port add(PortOffset offset)
Creates a new port with the integer value being the sum of this port plus the offset given.

Parameters:
offset - the offset to add.
Returns:
a new port.

getOffset

public PortOffset getOffset()
Generates an offset based on the last two decimal digits of this port. So 64432 yields an offset of 32 while 3250 yields an offset of 50.

Returns:
a standard offset.

offsetFrom

public PortOffset offsetFrom(Port port)
                      throws java.lang.NumberFormatException
Generates an offset based on the difference of this to the the argument.

Parameters:
port - the base port
Returns:
an offset from the given base port.
Throws:
java.lang.NumberFormatException - if the offset is negative or exceeds the maximum offset value.

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

compareTo

public int compareTo(Port o)
Specified by:
compareTo in interface java.lang.Comparable<Port>