home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-05-23 | 1.6 KB | 71 lines | [TEXT/CWIE] |
- package au.com.peter.libraries;
-
- import java.io.*;
- import java.net.Socket;
-
- public class TCPClient {
-
- public Socket connection = null;
- public OutputStream output;
- public InputStream input;
-
- public void OpenConnection( String hostport ) throws IOException, ParameterException {
- int p = hostport.indexOf( ':' );
- if ( p < 0 ) {
- throw new ParameterException( "Missing ':' in " + hostport );
- }
- String host = hostport.substring( 0, p );
- int port = Integer.parseInt( host.substring( p + 1 ) );
- DoOpenConnection( new Socket( host, port ) );
- }
-
- public void OpenConnection( String host, int port ) throws IOException {
- DoOpenConnection( new Socket( host, port ) );
- }
-
- public void DoOpenConnection( Socket conn ) throws IOException {
- if ( ConnectionIsOpen() ) {
- CloseConnection();
- }
- connection = conn;
- output = ConnectOutputStream( connection.getOutputStream() );
- input = ConnectInputStream( connection.getInputStream() );
- }
-
- public OutputStream ConnectOutputStream( OutputStream output ) {
- return new BufferedOutputStream( output );
- }
-
- public InputStream ConnectInputStream( InputStream input ) {
- return input;
- }
-
- public void CloseConnection() {
- if ( ConnectionIsOpen() ) {
- try {
- connection.close();
- } catch ( Exception e );
- connection = null;
- input = null;
- output = null;
- }
- }
-
- public void CheckOpen() throws IOException {
- if ( !ConnectionIsOpen() ) {
- throw new TCPClientException( "connection is not open" );
- }
- }
-
- public boolean ConnectionIsOpen() {
- return connection != null;
- }
-
- }
-
- class TCPClientException extends IOException {
- public TCPClientException( String s ) {
- super( s );
- }
- }
-