Logo Search packages:      
Sourcecode: wims version File versions

Connection.java

/*
$Id: Connection.java,v 1.2 2003/02/18 11:48:48 sander Exp $
*/


package fr.ove.utils;

import java.io.*;
import java.net.*;

/**
* A connection object.
* Opens a socket to the specified host on the specified port number.
*/
00015 public class Connection implements ConnectionConfigurable {
    /**
    * The socket of the connection
    */
00019     private Socket socket;

    /**
    * The input stream to read from the socket.
    */
00024     private InputStream inputStream = null;

    /**
    * The output stream to write into the socket.
    */
00029     private OutputStream outputStream = null;

    /**
    * The host name to connect.
    */
00034     private String hostName;

    /**
    * The port number.
    */
00039     private int portNumber;

    /**
    * The default constructor.
    * The default host name is "localhost" and the default port number is 6666.
    */
00045     public Connection() {
        this("localhost", 6666);
    }

    /**
    * The Constructor.
    * @param hostName the host name.
    * @param portNumber the port number.
    */
00054     public Connection(String hostName, int portNumber) {
        this.hostName = hostName;
        this.portNumber = portNumber;
    }

    /**
    * Sets the host name for the connection to create.
    * @param name the host name.
    */
00063     public void setHostName(String name) {
        hostName = name;
    }

    /**
    * Returns the host name for the connection to create.
    */
00070     public String getHostName() {
        return hostName;
    }

    /**
    * Sets the port number for the connexion to create.
    * @param number the port number.
    */
00078     public void setPortNumber(int number) {
        portNumber = number;
    }

    /**
    * Returns the port number for the connexion to create.
    */
00085     public int getPortNumber() {
        return portNumber;
    }

    /**
    * Opens the connection.
    */
00092     public void open() throws IOException {
            socket = new Socket(InetAddress.getByName(hostName), portNumber);
            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();
    }

    /**
    * Opens the connection through the specified socket.
    * @param socket the specified socket.
    */
00102     public void open(Socket socket) throws IOException {
            this.socket = socket;
            hostName = socket.getInetAddress().getHostName();
            portNumber = socket.getPort();
            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();
    }

    /**
    * Checks wether the connection is opened.
    */
00113     public boolean isOpened() {
        return ((outputStream != null) && (inputStream != null));
    }

    /**
    * Closes the connection.
    */
00120     public void close() throws IOException {
        outputStream.close();
        inputStream.close();
        outputStream = null;
        inputStream = null;
    }

    /**
    * Gets the input stream to read from the socket.
    */
00130     public InputStream getInputStream() {
        return inputStream;
    }

    /**
    * Gets the output stream to write into the socket.
    */
00137     public OutputStream getOutputStream() {
        return outputStream;
    }
}

Generated by  Doxygen 1.6.0   Back to index