This interface defines the socket stream connection.
A socket is accessed using a generic connection string with an explicit host
and port number. The host may be specified as a fully qualified host name or
IPv4 number. e.g. socket://host.com:79
defines a target socket
on the host.com
system at port 79
.
Note that RFC1900 recommends the use of names rather than IP numbers for best
results in the event of IP number reassignment.
Closing Streams
Every StreamConnection
provides a Connection
object
as well as an InputStream
and OutputStream
to
handle the I/O associated with the connection. Each of these interfaces has
its own close()
method. For systems that support duplex
communication over the socket connection, closing of the input or output
stream SHOULD shutdown just that side of the connection. e.g. closing the
InputStream
will permit the OutputStream
to
continue sending data.
Once the input or output stream has been closed, it can only be reopened with
a call to Connector.open()
. The application will receive an
IOException
if an attempt is made to reopen the stream.
BNF Format for Connector.open() string
The URI must conform to the BNF syntax specified below. If the URI does not
conform to this syntax, an IllegalArgumentException
is thrown.
<socket_connection_string> |
::= "socket://"<hostport> |
<hostport> |
::= host ":" port |
<host> |
::= host name or IP address (omitted for inbound connections, See
ServerSocketConnection) |
<port> |
::= numeric port number |
Examples
The following examples show how a SocketConnection
would be used
to access a sample loopback program.
SocketConnection sc = ( SocketConnection )Connector.open( "socket://host.com:79" );
sc.setSocketOption( SocketConnection.LINGER, 5 );
InputStream is = sc.openInputStream();
OutputStream os = sc.openOutputStream();
os.write( "\r\n".getBytes() );
int ch = 0;
while ( ch != -1 )
{
ch = is.read();
}
is.close();
os.close();
sc.close();