|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.net.tcp.SocketOptions
public class SocketOptions
A handy "bean" type wrapper around the standard Socket options.
This object provides a wrapper around the various Socket options which one can control. It is particularily handy if
you want to copy socket options from one Socket to another,
load options from a standard TagLookup table, format the current
options as a String, or apply the current settings
to an open Socket.
This class is used by Server to allow one to specify a
set of socket options to be applied for incoming connections.
It should be noted that JDK 1.3 added the Socket.setKeepAlive(boolean) option - this class doesn't support this as of
yet, but will once we move to a JDK 1.3 environment.
Server| Field Summary | |
|---|---|
static int |
DISABLE_SO_LINGER
Used as a parameter to setSoLinger(int) to
disable SO_LINGER. |
| Constructor Summary | |
|---|---|
SocketOptions()
|
|
| Method Summary | |
|---|---|
Exception |
apply(ServerSocket s)
Apply the settings to an open ServerSocket
connection. |
SocketException |
apply(Socket s)
Apply the settings to an open Socket connection. |
Socket |
createClientSocket()
Create a client socket object based upon the current settings in the bean. |
Socket |
createSocket(TagLookup tl,
String dhost,
int dport)
Fully construct and initialize a TCP socket based
on a set of properties. |
static InetAddress |
getInetAddress(String s)
Helper method to convert string to internet address. |
InetAddress |
getLocalHost()
Get the local host to connect to if creating a client socket. |
int |
getLocalPort()
Get the port number to connect to when making a local connection. |
void |
getProperties(Properties props,
String tag)
Set the value of the object from values specified in a TagLookup table. |
int |
getReceiveBufferSize()
Get the transmit buffer size
(in bytes). |
InetAddress |
getRemoteHost()
Get the remote host to connect to if creating a client socket. |
int |
getRemotePort()
Get the port number to connect to when making a remote connection. |
int |
getSendBufferSize()
Get the transmit buffer size
(in bytes). |
int |
getSoLinger()
Get the linger time (in seconds). |
int |
getSoTimeout()
Get the timeout value for the
connection (in seconds). |
boolean |
isTcpNoDelay()
Is whether TCP_NO_DELAY is enabled set? |
void |
setLocalHost(InetAddress val)
Set the local host to connect to if creating a client socket. |
void |
setLocalPort(int val)
Set the port number to connect to when making a local connection. |
Exception |
setProperties(TagLookup tl)
Set the value of the object from values specified in a TagLookup table. |
void |
setReceiveBufferSize(int val)
Set the transmit buffer size
(in bytes). |
void |
setRemoteHost(InetAddress val)
Set the remote host to connect to if creating a client socket. |
void |
setRemotePort(int val)
Set the port number to connect to when making a remote connection. |
void |
setSendBufferSize(int val)
Set the transmit buffer size
(in bytes). |
void |
setSoLinger(int val)
Set the linger time (in seconds). |
void |
setSoTimeout(int val)
Set the timeout value for the
connection (in milliseconds). |
void |
setTcpNoDelay(boolean val)
Set whether TCP_NO_DELAY is enabled. |
Exception |
setValues(Socket s)
Fetch the TCP settings from an open Socket. |
String |
toString()
Get String representation of the object. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final int DISABLE_SO_LINGER
setSoLinger(int) to
disable SO_LINGER.
setSoLinger(int),
Constant Field Values| Constructor Detail |
|---|
public SocketOptions()
| Method Detail |
|---|
public Socket createSocket(TagLookup tl,
String dhost,
int dport)
throws ParseException,
IOException,
UnknownHostException
TCP socket based
on a set of properties.
This method does just about everything for you when you need to create a TCP client socket from a set of properties. It does the following:
Sets the properties of the object
based upon the properties you pass.Socket object using host/port
information from the properties passed (or your default
values).Applies the TCP connection properties to the
newly created Socket object.Socket
object to you.Many error conditions could be encountered along the way. If an error is encountered, a exception is thrown. If no exception is thrown, then you will receive a fully configured socket (null is never returned).
You should refer to the setProperties
method for all of the standard socket options which are
recognized. In addition to the standard options, this method also
recognizes (and sometimes requires) the following:
tl - The properties to lookup the configuration information for the
connection.dhost - Default remote host name to use (may be null if you want to
require that the host be specified in the properties passed).dport - Default remote port to connect to (set to -1 if you want to
require that the "port" value be specified in the properties
passed).
Socket which is fully configured and initialized.
ParseException - If there was a problem parsing any of the values from the
properties.
UnknownHostException - If the host specified was not known (failed to lookup).
IOException - One of the many I/O errors that can occur when creating a
socket connection.
public Socket createClientSocket()
throws IllegalStateException,
IOException
This method attempts to create a Socket based
upon the current settings in the object. At a minimum, both the
remote host and remote port must be set to valid values.
Socket which is fully configured and
initialized and ready to use.
IllegalStateException - If the remote host or remote port have not been set.
IOException - This is typically a SocketException
indicating there is a problem establishing the socket.public String toString()
setProperties(TagLookup).
toString in class ObjectsetProperties(TagLookup)public Exception setProperties(TagLookup tl)
TagLookup table.
This is a very flexible method to set the values contained in
this object based upon the values retrieved from the passed
TagLookup table (which can originate from about
any source - command line, applet parameters, property files,
etc). All values are optional - if not specified in the property
file, then this object won't update its current settings for the
omitted fields (this is important to understand when the values
are applied). The following properties are
recognized:
receiveBufferSize=SIZEsendBufferSize=SIZEsoLinger=SECSsoTimeout=MILLIStcpNoDelay=true|false"no delay"
option should be applied to the connection.
tl - The TagLookup table to fetch properties for.apply(java.net.Socket)
public void getProperties(Properties props,
String tag)
TagLookup table.
This is a very flexible method to set the values contained in
this object based upon the values retrieved from the passed
TagLookup table (which can originate from about
any source - command line, applet parameters, property files,
etc). All values are optional - if not specified in the property
file, then this object won't update its current settings for the
omitted fields (this is important to understand when the values
are applied). The following properties are
recognized:
receiveBufferSize=SIZEsendBufferSize=SIZEsoLinger=SECSsoTimeout=MILLIStcpNoDelay=true|false"no delay"
option should be applied to the connection.
tl - The TagLookup table to fetch properties for.apply(java.net.Socket)public Exception setValues(Socket s)
Socket.
This method is useful if you want to fetch the socket options
from an already open socket. This can be useful to do the
following:
representation of the socket
options.Apply the settings of one Socket to another.remote host and remote port settings associated with the
socket.local host and local port settings associated with the
socket.
s - The Socket which you want to fetch the settings from.
setProperties(TagLookup)public SocketException apply(Socket s)
Socket connection.
This method will apply all of the settings, which have been
explicitly set, to the Socket passed to the
method. For example, if the setSendBufferSize(int) method had
been previously invoked, then the size specified would be applied
to the Socket. Refer to the various "set" methods to see
how one can go about setting the options.
s - Socket to apply the current settings to.
setValues(Socket)public Exception apply(ServerSocket s)
ServerSocket
connection.
This method will apply all of the settings, which have been
explicitly set, to the ServerSocket passed
to the method. Currently, the only setting which can be applied
to a ServerSocket are is the SO_TIMEOUT option.
s - ServerSocket to apply the current settings to.
apply(Socket)public void setTcpNoDelay(boolean val)
TCP_NO_DELAY is enabled.
val - New boolean value to assign.
see #getTcpNoDelaypublic boolean isTcpNoDelay()
TCP_NO_DELAY is enabled set?
public void setSoLinger(int val)
linger time (in seconds).
val - New int value to assign (in seconds). Pass DISABLE_SO_LINGER value to disable.getSoLinger()public int getSoLinger()
linger time (in seconds).
DISABLE_SO_LINGER if disabled.setSoLinger(int)public void setSoTimeout(int val)
timeout value for the
connection (in milliseconds).
val - Maximum number of milliseconds to wait on a read. Pass 0 to
block forever. Use -1 to disable (which means we don't adjust
whatever the system gives us).getSoTimeout()public int getSoTimeout()
timeout value for the
connection (in seconds).
setSoTimeout(int)public void setSendBufferSize(int val)
transmit buffer size
(in bytes).
val - New receive buffer size (in bytes). Set to 0 to clear setting
and go back to sytem defaults.getSendBufferSize()public int getSendBufferSize()
transmit buffer size
(in bytes).
setSendBufferSize(int)public void setReceiveBufferSize(int val)
transmit buffer size
(in bytes).
val - New receive buffer size (in bytes). Set to 0 to clear setting
and go back to sytem defaults.getReceiveBufferSize()public int getReceiveBufferSize()
transmit buffer size
(in bytes).
setReceiveBufferSize(int)public void setRemoteHost(InetAddress val)
val - New InetAddress value to assign, or null to clear.getRemoteHost()public InetAddress getRemoteHost()
setRemoteHost(java.net.InetAddress)public void setRemotePort(int val)
val - New int value to assign - set to 0 or less to clear.getRemotePort()public int getRemotePort()
setRemotePort(int)public void setLocalHost(InetAddress val)
Typically one never bothers to set this. If your system has multiple IP addresses, it allows one to specify which IP address you want your local side of the connection to be bound to.
val - New InetAddress value to assign, or null to clear.getLocalHost()public InetAddress getLocalHost()
setLocalHost(java.net.InetAddress)public void setLocalPort(int val)
val - New int value to assign - set to 0 or less to clear.getLocalPort()public int getLocalPort()
setLocalPort(int)
public static InetAddress getInetAddress(String s)
throws UnknownHostException
Unfortunately, it can be difficult to transform a string value into a Internet address (and vice versa). In one case, one can always use the absolute address (like: 127.0.0.1), but this is not very nice and doesn't help when servers have their addresses changed. One can also use the fully qualified domain name (like: my.yahoo.com), however, this has a problem if the name can't be resolved.
This method attempts to solve the problem by allowing one to specify a "slash" separated list of names to try and resolve. For example, you could pass "localhost/127.0.0.1" and we would first try to use "localhost" if that failed, we would try "127.0.0.1".
s - String to convert to a internet address (must not be null)
UnknownHostException - If unable to resolve string to a internet address.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||