ewe.net
Class Socket

java.lang.Object
  extended byewe.util.Errorable
      extended byewe.io.BasicStreamObject
          extended byewe.io.StreamObject
              extended byewe.net.SocketBase
                  extended byewe.net.Socket
All Implemented Interfaces:
BasicStream, OverridesClose, Stream, Streamable, StreamCanPause

public class Socket
extends SocketBase
implements StreamCanPause, OverridesClose

A Socket is used to make a TCP/IP streaming connection to a remote host.

For devices which have an infra red port and support IrSock (e.g. WinCE devices) specifying a host name of "infra-red" will tell the socket to connect to any available device on the infra-red port.


Nested Class Summary
 
Nested classes inherited from class ewe.net.SocketBase
SocketBase.SocketInputStream, SocketBase.SocketOutputStream
 
Field Summary
 
Fields inherited from class ewe.net.SocketBase
inputIsShutdown, inputStream, outputIsShutdown, outputStream, remoteHost, remotePort
 
Fields inherited from class ewe.io.StreamObject
napIterations, napTime, READWRITE_CLOSED, READWRITE_ERROR, READWRITE_WOULDBLOCK
 
Fields inherited from class ewe.io.BasicStreamObject
closed
 
Fields inherited from class ewe.util.Errorable
error
 
Fields inherited from interface ewe.io.StreamCanPause
PAUSE_UNTIL_CAN_READ, PAUSE_UNTIL_CAN_WRITE, PAUSE_UNTIL_CLOSED, PAUSE_UNTIL_FLUSHED
 
Constructor Summary
Socket(InetAddress host, int port)
          Opens a socket.
Socket(String host, int port)
          Opens a socket.
Socket(String host, int port, IOHandle handle)
          Opens a socket.
 
Method Summary
 boolean close()
          Closes the socket.
 boolean flushStream()
          This is non-blocking.
 InetAddress getInetAddress()
          Get the remote host the socket is connected to.
 InputStream getInputStream()
          Get an InputStream for reading from the connected Socket.
 InetAddress getLocalAddress()
          Get the address of the local host the socket is bound to.
 int getLocalPort()
          Get the local port the socket is bound to.
 OutputStream getOutputStream()
          Get an OutputStream for writing to the connected Socket.
 int getPort()
          Get the remote port the socket is connected to.
protected  int getSocketParameter(int par)
           
 boolean isOpen()
          Returns true if the socket is open and false otherwise.
 int nonBlockingRead(byte[] buf, int start, int count)
          This returns: >0 = Number of bytes read.
 int nonBlockingWrite(byte[] buf, int start, int count)
          This returns: >0 = Number of bytes written.
 int pauseUntilReady(int pauseType, int time)
           
protected  int setSocketParameter(int par, boolean booleanValue, int intValue)
           
 InputStream toInputStream()
           
 OutputStream toOutputStream()
           
 
Methods inherited from class ewe.net.SocketBase
getKeepAlive, getReceiveBufferSize, getSendBufferSize, getSoLinger, getTcpNoDelay, setKeepAlive, setReceiveBufferSize, setSendBufferSize, setSoLinger, setTcpNoDelay, shutdownInput, shutdownOutput
 
Methods inherited from class ewe.io.StreamObject
closeStream, doRead, doWrite, flush, nap, nap, read, readAByte, readBytes, readBytes, write, writeAByte, writeBytes, writeBytes
 
Methods inherited from class ewe.io.BasicStreamObject
getException, getName, read, read, throwIOException, toStream, write, write
 
Methods inherited from class ewe.util.Errorable
returnError, returnError, returnError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Constructor Detail

Socket

public Socket(String host,
              int port)
       throws IOException,
              UnknownHostException
Opens a socket. This method establishes a socket connection by looking up the given host and performing the 3 way TCP/IP handshake. This is the blocking version of the constructor - may be used in a thread without blocking other threads, however there is no way to abort the process.

If you want to connect to a device via the infra-red port, set the host to be "infra-red".

Parameters:
host - the host name or IP address to connect to.
port - the port number to connect to.

Socket

public Socket(InetAddress host,
              int port)
       throws IOException
Opens a socket. This method establishes a socket connection by looking up the given host and performing the 3 way TCP/IP handshake. This is the blocking version of the constructor - may be used in a thread without blocking other threads, however there is no way to abort the process.

If you want to connect to a device via the infra-red port, set the host to be "infra-red".

Parameters:
host - the host address to connect to.
port - the port number to connect to.

Socket

public Socket(String host,
              int port,
              IOHandle handle)
Opens a socket. This method establishes a socket connection by looking up the given host and performing the 3 way TCP/IP handshake. This is the non-blocking version of the constructor. It will return immediately and you must check the IOHandle for successful connection. Passing a null handle will make this a blocking call. The IOHandle can be used to abort the connection by calling its stop() method.

If you want to connect to a device via the infra-red port, set the host to be "infra-red".

Parameters:
host - the host name or IP address to connect to
port - the port number to connect to
handle - a handle to use for checking for successful connection.
Method Detail

close

public boolean close()
Closes the socket. Returns true if the operation is successful and false otherwise.

Specified by:
close in interface BasicStream
Overrides:
close in class StreamObject

isOpen

public boolean isOpen()
Returns true if the socket is open and false otherwise. This can be used to check if opening the socket was successful.

Specified by:
isOpen in interface BasicStream
Overrides:
isOpen in class StreamObject

getPort

public int getPort()
Get the remote port the socket is connected to.


getInetAddress

public InetAddress getInetAddress()
Get the remote host the socket is connected to.


getLocalAddress

public InetAddress getLocalAddress()
Get the address of the local host the socket is bound to.


getLocalPort

public int getLocalPort()
Get the local port the socket is bound to.


nonBlockingRead

public int nonBlockingRead(byte[] buf,
                           int start,
                           int count)
This returns: >0 = Number of bytes read. 0 = No bytes ready to read. -1 = Stream closed. -2 = IO error.

Specified by:
nonBlockingRead in interface BasicStream
Overrides:
nonBlockingRead in class StreamObject
Parameters:
buf - Destination byte array to hold incoming data.
start - Starting index in buff for incoming data.
count - Maximum number of bytes to read - should never be zero.
Returns:
greater than 0 = Number of bytes read.
0 = No bytes available to read at this time.
-1 = Stream end reached no further bytes to read.
-2 = IO Error.
Note that these values are different to the Stream.readBytes method.

nonBlockingWrite

public int nonBlockingWrite(byte[] buf,
                            int start,
                            int count)
This returns: >0 = Number of bytes written. 0 = No bytes could be written yet. -1 = Stream closed. -2 = IO error.

Specified by:
nonBlockingWrite in interface BasicStream
Overrides:
nonBlockingWrite in class StreamObject
Parameters:
buf - Source byte array holding data to be written.
start - Starting index in buff for data to be written.
count - Number of bytes to write - should never be zero.
Returns:
greater than 0 = Number of bytes actually written.
0 = No bytes could be written yet - but the stream is still open.
-1 = Stream has been closed - no further writes are possible.
-2 = IO error - something went wrong.
Note that these values are different to the Stream.writeBytes method.

flushStream

public boolean flushStream()
                    throws IOException
Description copied from interface: BasicStream
This is non-blocking. It returns true if the flush completed, false if it did not, or throws an exception on error.

Specified by:
flushStream in interface BasicStream
Throws:
IOException

pauseUntilReady

public int pauseUntilReady(int pauseType,
                           int time)
Specified by:
pauseUntilReady in interface StreamCanPause

getInputStream

public InputStream getInputStream()
Get an InputStream for reading from the connected Socket.


getOutputStream

public OutputStream getOutputStream()
Get an OutputStream for writing to the connected Socket.


toOutputStream

public OutputStream toOutputStream()
Specified by:
toOutputStream in interface Stream
Overrides:
toOutputStream in class BasicStreamObject

toInputStream

public InputStream toInputStream()
Specified by:
toInputStream in interface Stream
Overrides:
toInputStream in class BasicStreamObject

getSocketParameter

protected int getSocketParameter(int par)
Specified by:
getSocketParameter in class SocketBase

setSocketParameter

protected int setSocketParameter(int par,
                                 boolean booleanValue,
                                 int intValue)
Specified by:
setSocketParameter in class SocketBase