Holger's Java API

## com.antelmann.net
Class SocketConnection

    java.lang.Object
      com.antelmann.net.SocketConnection

All Implemented Interfaces:
  NetConnection, Closeable

Direct Known Subclasses:
  SSLSocketConnection

    @Todo(value="support streaming via e.g. BinaryData")
    public class SocketConnection

extends Object
implements NetConnection

A SocketConnection provides simplified access to a remote process through a Socket connection. To create a SocketConnection, use createNetConnection()

Author:
  Holger Antelmann
See Also:
  `NetConnectionServer`, `NetConnectionHandler`, `ConnectionDispatcher`, `NetConnection`

Field Summary
`protected boolean` `autoReset`
`protected InetAddress` `host`
`protected Handshake` `hs`
`protected ObjectInputStream` `in`
`protected Logger` `logger`
`protected ObjectOutputStream` `out`
`protected int` `port`
`protected Socket` `socket`

Constructor Summary
`protected` `SocketConnection(Socket socket, Handshake hs)`

Method Summary
`void` `close()` closes the SocketConnection
`static SocketConnection` `createConnection(String hostname, int port)`
`static SocketConnection` `createConnection(String hostname, int port, Handshake hs)` returns a SocketConnection provided there is a Server listening at the specified location with the secified signature; note that this method is blocking.
`static SocketConnection` `createServerConnection(int port, Handshake hs)`
`static SocketConnection` `createServerConnection(ServerSocket serverSocket, Handshake hs)` waits and listens for a single connection to be made and returns a SocketConnection if the connection made matches the signature.
`static SocketConnection` `createServerConnection(Socket socket, Handshake hs)` creates a connection based on a socket that results from a ServerSocket.accept()
`Handshake` `getHandshake()` returns the identification type object for this connection; this signature is the same on both sides of the connection.
`int` `getLocalPort()`
`Logger` `getLogger()` loggs standard events - no exceptions are logged here
`InetAddress` `getRemoteHost()`
`int` `getRemotePort()`
`Socket` `getSocket()` returns the underlying Socket of this connection
`boolean` `isActive()` returns whether this NetConnection is currently active
`boolean` `isAutoReset()`
`Object` `readMessage()` reads an object from the connection (operation blocks until object is read)
`void` `reconnect()` applies to the client side of a connection rather than a server
`void` `reconnect(int timeout)` applies to the client side of a connection rather than a server; the timeout only appies to the handshake.
`void` `reset()` performs a manual `reset()` on the `ObjectOutputStream`.
`void` `sendMessage(Object message)` sends an object over the connection
`void` `setAutoReset(boolean flag)`
`void` `setLogger(Logger logger)`
`void` `setSoTimeout(int timeout)` propagates to the embedded socket
`String` `toString()`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

Field Detail

### socket

    protected Socket socket

### host

    protected InetAddress host

### port

    protected int port

### in

    protected ObjectInputStream in

### out

    protected ObjectOutputStream out

### hs

    protected Handshake hs

### logger

    protected Logger logger

### autoReset

    protected boolean autoReset

Constructor Detail

### SocketConnection

    protected SocketConnection(Socket socket,
                               Handshake hs)
                        throws IOException

Throws:
  `IOException`

Method Detail

### createServerConnection

    public static SocketConnection createServerConnection(int port,
                                                          Handshake hs)
                                                   throws IOException,
                                                          HandshakeException

  Throws:
    `IOException`
    `HandshakeException`

### createServerConnection

    public static SocketConnection createServerConnection(ServerSocket serverSocket,
                                                          Handshake hs)
                                                   throws IOException,
                                                          HandshakeException

  waits and listens for a single connection to be made and returns a SocketConnection if the connection made matches the signature. The soTimeout is only in effect during the handshake and will be set to 0 thereafter.

  Throws:
    `HandshakeException` - if the signatures did not match
    `IOException`

### createServerConnection

    public static SocketConnection createServerConnection(Socket socket,
                                                          Handshake hs)
                                                   throws IOException,
                                                          HandshakeException

  creates a connection based on a socket that results from a ServerSocket.accept()

  Throws:
    `IOException`
    `HandshakeException`

### createConnection

    public static SocketConnection createConnection(String hostname,
                                                    int port)
                                             throws IOException,
                                                    HandshakeException

  Throws:
    `IOException`
    `HandshakeException`

### createConnection

    public static SocketConnection createConnection(String hostname,
                                                    int port,
                                                    Handshake hs)
                                             throws IOException,
                                                    HandshakeException

  returns a SocketConnection provided there is a Server listening at the specified location with the secified signature; note that this method is blocking.

  Parameters:
    `hostname` - specifies the server to connect to
    `port` - specifies the port at the server to connect to
    `hs` - an object that identifies the desired type of connection
  Throws:
    `HandshakeException` - if the signatures did not match
    `IOException`
  See Also:
    `NetConnectionServer`

### isActive

    public boolean isActive()

  Description copied from interface: `NetConnection`
  returns whether this NetConnection is currently active

  Specified by:
    `isActive` in interface `NetConnection`

### reconnect

    public void reconnect()
                   throws IOException,
                          HandshakeException

  applies to the client side of a connection rather than a server

  Throws:
    `IOException`
    `HandshakeException`

### reconnect

    public void reconnect(int timeout)
                   throws IOException,
                          HandshakeException

  applies to the client side of a connection rather than a server; the timeout only appies to the handshake.

  Throws:
    `IOException`
    `HandshakeException`

### sendMessage

    public void sendMessage(Object message)
                     throws IOException

  Description copied from interface: `NetConnection`
  sends an object over the connection

  Specified by:
    `sendMessage` in interface `NetConnection`
  Throws:
    `IOException`

### setAutoReset

    public void setAutoReset(boolean flag)

### isAutoReset

    public boolean isAutoReset()

### reset

    public void reset()
               throws IOException

  performs a manual `reset()` on the `ObjectOutputStream`. An alternative is to set autoReset on this object to have the stream reset on each message send. This is necessary to allow the same object to be written more than once while the state of the object changes.

  Throws:
    `IOException`
  See Also:
    `setAutoReset(boolean)`, `ObjectOutputStream.reset()`

### readMessage

    public Object readMessage()
                       throws IOException,
                              ClassNotFoundException

  Description copied from interface: `NetConnection`
  reads an object from the connection (operation blocks until object is read)

  Specified by:
    `readMessage` in interface `NetConnection`
  Throws:
    `IOException`
    `ClassNotFoundException`

### setSoTimeout

    public void setSoTimeout(int timeout)
                      throws IOException

  propagates to the embedded socket

  Throws:
    `IOException`

### getHandshake

    public Handshake getHandshake()

  Description copied from interface: `NetConnection`
  returns the identification type object for this connection; this signature is the same on both sides of the connection. Note that this object requires the equals(Object obj) method to work after serialization/de-serialization; Strings work just fine.

  Specified by:
    `getHandshake` in interface `NetConnection`

### close

    public void close()
               throws IOException

  closes the SocketConnection

  Specified by:
    `close` in interface `Closeable`
  Throws:
    `IOException`

### getLogger

    public Logger getLogger()

  loggs standard events - no exceptions are logged here

### setLogger

    public void setLogger(Logger logger)

### getSocket

    public Socket getSocket()

  Description copied from interface: `NetConnection`
  returns the underlying Socket of this connection

  Specified by:
    `getSocket` in interface `NetConnection`

### getRemoteHost

    public InetAddress getRemoteHost()

### getRemotePort

    public int getRemotePort()

### getLocalPort

    public int getLocalPort()

### toString

    public String toString()

  Overrides:
    `toString` in class `Object`

(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads