|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.net.URLConnection
com.ccg.net.tcp.URLConnection
public class URLConnection
Implementation of a URLConnection for TCP connections.
This is a implementation of a "java.protocol.handler.pkgs"
object which will add a "tcp:" type to the type of URL
objects that the JVM recognizes. This allows one to open up input and output streams on a TCP
connection in a manner similar to how one would go about doing it
for a "http:" type of URL.
This class is used to handle both the creation of connections
(connecting to another machine via TCP), and the acceptance of
connections (waiting for another machine to connect to this
machine). However, this is a point to point type of connection. If
you want to create a standard TCP server that can handle many
clients at once, you will want to look at the Server
class instead of this one.
The form of the URL which this class adds support
for looks like the following:
tcp://[HOST]:PORT/[OPTIONS]
SocketOptions.setProperties(com.ccg.util.TagLookup) are
allowed. I've tried to list them below, but you should check with
that class as well in case they change over time (JDK 1.3 is going
to support the TCP keep alive option).
receiveBufferSize=SIZEsendBufferSize=SIZEsoLinger=SECSsoTimeout=MILLIStcpNoDelay=true|false"no delay" option should be applied to the
connection.
You might want to take a look at the Cat class for more
information on how one uses this type of URL (the source code would
be handy for developers), but here are some quick examples:
"tcp://:20080/" "tcp://www.yahoo.com:80/sendBufferSize=5000,soLinger=3,tcpNoDelay=true"
To make this class available to ALL Java code which opens up
URL's, you will need to add the com.ccg.net package to the
list of valid protocol handlers. You should be able to configure
the JVM to support this, or add it as a definition when you start
Java (I know its possible, but I've forgotten how to do it at the
moment). If you'd rather just enable it directly in your code, you
can add the following statements to add the com.ccg.net
based protocol handlers to the JVM's search path:
Class.forName("com.ccg.net.tcp.URLConnection");
To enable it from the command line, you can use "-Djava.protocol.handler.pkgs=com.ccg.net" to the JVM invocation to add it to the System properties (it may even be possible to locate your JVM's system property configuration file and add it there by hand). This has the advantage of making the protocol available to ALL Java code as a standard URL handler. This is demonstrated with the following command line invocation:
java -Djava.protocol.handler.pkgs=com.ccg.net com.ccg.app.convert.HexDump \
-in tcp://localhost:23/soTimeout=5000
URLConnection,
Cat| Field Summary |
|---|
| Fields inherited from class java.net.URLConnection |
|---|
allowUserInteraction, connected, doInput, doOutput, ifModifiedSince, url, useCaches |
| Constructor Summary | |
|---|---|
URLConnection(URL url)
Initializes object with a standard URL
This method is used to initialize the connection, however, no
attempt will be made for a connection until the connect(java.net.URL, com.ccg.net.tcp.SocketOptions),
getInputStream(), or getOutputStream() methods are
invoked. |
|
| Method Summary | |
|---|---|
void |
connect()
Create the connection to the specified URL. |
static Socket |
connect(URL url,
SocketOptions so)
Static method to open up a TCP connection. |
InputStream |
getInputStream()
Get the InputStream to read data from the remote end. |
OutputStream |
getOutputStream()
Get the OutputStream to send data to the remote end. |
Socket |
getSocket()
Access the Socket which is established after connect(java.net.URL, com.ccg.net.tcp.SocketOptions)
Typially one will just use the getInputStream() or getOutputStream() methods. |
SocketOptions |
getSocketOptions()
Get the socket options to apply to all incoming connections |
void |
setSocketOptions(SocketOptions val)
Set the socket options to apply to all incoming connections
This method allows one to specify a specific set of socket options (receive buffer size, send buffer
size, etc), which should be applied to all incoming connections. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public URLConnection(URL url)
URL
This method is used to initialize the connection, however, no
attempt will be made for a connection until the connect(java.net.URL, com.ccg.net.tcp.SocketOptions),
getInputStream(), or getOutputStream() methods are
invoked.
url - The URL to initialize the connection with - it
must follow the form specified by the class.URLConnection(URL)| Method Detail |
|---|
public static Socket connect(URL url,
SocketOptions so)
throws IOException
class
overview. It parses the fields from the passed URL,
establishes a connection to the specified HOST:PORT, or waits for
a connection to be made to the specified :PORT, sets any
specified socket options and returns the
newly formed connection.
url - The URL in the proper form recognized by this
class - must not be null.so - If you have any default SocketOptions which you would
like to apply to the connection once its established. It
should be noted, that any options found in the URL will
override values found in this object. You can pass null if you
just want the default values.
Socket to the newly opened connection.
IOException - If we were unable to setup the socket.connect()
public void connect()
throws IOException
URL.
This method establishes the connection specified by the URL. As explained in the class
overview this may mean that we will make a TCP connection to a
specific host and port, OR, we will listen on a specific port and
wait for someone else to connect to us.
If this method succeeds (doesn't throw an IOException), then
its safe to assume that the connection was established. This
method uses the static
version to actually open the connection.
Typically, one would set the socket
options prior to invoking this method - but we will try to honor
requests that come after the connection is already established.
connect in class URLConnectionIOException - If there is a problem establishing the connection.getInputStream(),
getOutputStream()public Socket getSocket()
Socket which is established after connect(java.net.URL, com.ccg.net.tcp.SocketOptions)
Typially one will just use the getInputStream() or getOutputStream() methods. However, if one is curious about the
underlying connection, then this method can be used to retrieve
the underlying Socket object. This method will return
null if a connection hasn't been established yet.
Socket established by connect(java.net.URL, com.ccg.net.tcp.SocketOptions), or null if
one hasn't been established yet.connect(java.net.URL, com.ccg.net.tcp.SocketOptions)
public InputStream getInputStream()
throws IOException
InputStream to read data from the remote end.
This method will make the connection, if
necessary, and then return a InputStream
object which one can use to read the data sent from the remote
end of the connection.
getInputStream in class URLConnectionInputStream which you can read the raw
data sent from the remote end of the connection.
IOException - If there is a problem establishing the connection, or getting
a InputStream from the Socket.connect(java.net.URL, com.ccg.net.tcp.SocketOptions)
public OutputStream getOutputStream()
throws IOException
OutputStream to send data to the remote end.
This method will make the connection, if
necessary, and then return a OutputStream
object which one can use to send data to the remote end of the
connection.
getOutputStream in class URLConnectionOutputStream which you can use to send
data to the remote end of the connection.
IOException - If there is a problem establishing the connection, or getting
a OutputStream from the Socket.connect(java.net.URL, com.ccg.net.tcp.SocketOptions)public void setSocketOptions(SocketOptions val)
socket options to apply to all incoming connections
This method allows one to specify a specific set of socket options (receive buffer size, send buffer
size, etc), which should be applied to all incoming connections. By default, this will be null indicating that no
special options should be applied.
val - New SocketOptions value to assign (pass null to leave
connections in the system default state).getSocketOptions()public SocketOptions getSocketOptions()
socket options to apply to all incoming connections
setSocketOptions(com.ccg.net.tcp.SocketOptions)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||