|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.net.Socket | +--com.ibm.etill.framework.io.ClientSocket | +--com.ibm.etill.framework.io.TypicalClientSocket
This is a convenience class. It exists for the following reason: When a TCP connection is closed gracefully by the other end, a FIN is sent to this end's TCP stack causing an end-of-stream condition when all data has been read. Most applications, when the other end closes its connection gracefully, wish to recognize this as soon as possible and not be allowed to write data to the socket anymore. This socket, when read from, throws IOExceptions when it gets an end-of-stream instead of returning -1. It also remembers if an IOException was thrown, so that later writes to the socket to fail. This is the scenario that, I believe, most programmers wish to see happen when writing most applications ... and is especially useful when multiple threads use the same socket and would like writes to fail as soon as reads have failed (because end-of-stream was received) on another thread.
Field Summary | |
---|---|
static int |
READBYTE_NO_DATA
No data is available from socket, but the socket is still up |
Constructor Summary | |
---|---|
TypicalClientSocket(Socket s)
Class constructor that takes an existing socket. |
|
TypicalClientSocket(TcpipHost targetHost)
Class constructor taking a TcpipHost. |
|
TypicalClientSocket(TcpipHost targetHost,
TcpipHost socksHost)
Class constructor taking a TcpipHost as the target and a TcpipHost socks server. |
Method Summary | |
---|---|
int |
available()
returns total number of bytes ready to be read from the socket. |
void |
checkForJavaMemoryLeakOnRead()
Continually reads from the socket unless an IOException occurs This function is used for testing for a memory leak "caused" by reading from the socket. |
void |
close()
closes the socket. |
void |
flush()
flushes data to the socket. |
InetAddress |
getInetAddress()
Gets the socket's InetAddress . |
InputStream |
getInputStream()
Gets the socket's InputStream . |
InetAddress |
getLocalAddress()
Gets the socket's local InetAddress . |
int |
getLocalPort()
Gets the socket's local port number. |
OutputStream |
getOutputStream()
Gets the socket's OutputStream . |
int |
getPort()
Gets the socket's port number. |
int |
getSoLinger()
Returns setting for SO_LINGER. |
int |
getSoTimeout()
Gets the setting for the socket timeout. |
boolean |
getTcpNoDelay()
Tests if TCP_NODELAY is enabled. |
boolean |
isDown()
|
boolean |
isUp()
returns whether the socket is up or down. |
static void |
main(String[] args)
|
void |
notifyIfSocketIsDown(SocketDownInterface n)
Accepts an Object implementing the SocketDownInterface to be notified when the socket interface goes down. |
int |
read(byte[] ba,
int offset,
int maxLengthToRead)
Gets a set of bytes. |
int |
readByte()
gets a byte from the socket |
int |
readByteNoBlocking()
gets a byte from the socket without blocking the current thread when no data is present. |
void |
setSoLinger(boolean on,
int val)
Enable/disable SO_LINGER with the specified linger time in seconds. |
void |
setSoTimeout(int timeout)
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. |
void |
setTcpNoDelay(boolean on)
Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). |
String |
toString()
Gets the socket in the form of a string. |
void |
write(byte thebyte)
writes a byte to the socket. |
void |
write(byte[] bytes)
writes a set of bytes to the socket. |
Methods inherited from class java.net.Socket |
---|
getKeepAlive, getReceiveBufferSize, getSendBufferSize, setKeepAlive, setReceiveBufferSize, setSendBufferSize, setSocketImplFactory, shutdownInput, shutdownOutput |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final int READBYTE_NO_DATA
Constructor Detail |
public TypicalClientSocket(TcpipHost targetHost) throws IOException
targetHost
- the host and port to connect toIOException
- if an InputStream
or an OutputStream
can not be obtained from the socket.public TypicalClientSocket(TcpipHost targetHost, TcpipHost socksHost) throws IOException
targetHost
- the host and port to connect tosocksHost
- the socks server and port to connect to.IOException
- if an InputStream or an OutputStream can not be obtained from the
socket.public TypicalClientSocket(Socket s) throws IOException
serversocket.accept()
call.
DO NOT use the InputStream and OutputStream objects *from the socket* you gave on this
constructor. You must use the input/output streams gotten from calling
getInputStream()
and getOutputStream()
in this class.s
- an existing socketIOException
- if an InputStream or an OutputStream can not be obtained from the
socket.Method Detail |
public void notifyIfSocketIsDown(SocketDownInterface n)
n
- an object that implements the SocketDownInterface
will be
notified if the socket interface goes down.public boolean isUp()
true
if the socket is still up or
false
if the socket is down.public boolean isDown()
public int readByte() throws IOException
READBYTE_NO_DATA
if no data is
available to be read from the socket. if data is available to read from
the socket, a value from 0
to 255
will be returned.IOException
- if the other side closed its connectionpublic void checkForJavaMemoryLeakOnRead() throws IOException
IOException
- if the socket closed (throws an IOException). Note that the
the socket is down using noteThatSocketIsDown
.public int readByteNoBlocking() throws IOException
READBYTE_NO_DATA
if no data is
available to be read from the socket. if data is available to read from
the socket, a value from 0
to 255
will be returned.IOException
- if the socket is already down or if readByte()
throws one.
public int read(byte[] ba, int offset, int maxLengthToRead) throws IOException
ba
- where the set of bytes will be placed.offset
- offset in ba where the set of bytes will be placed.maxLengthToRead
- the number of bytes to read.0
to 255
will be returned.IOException
- if the socket is already down or the socket goes down while
the set of bytes is being loaded into ba..public int available() throws IOException
IOException
- if the socket is already down or an exception occurs
attempting to determine how many bytes the socket
has ready to read.public void write(byte[] bytes) throws IOException
bytes
- an array of bytes to write to the socketIOException
- if the socket is already down or an exception occurs
attempting to determine how many bytes the socket
has ready to read.public void write(byte thebyte) throws IOException
thebyte
- the byte to write to the socketIOException
- if the socket is already down or an exception occurs
attempting to determine how many bytes the socket
has ready to read.public void flush() throws IOException
IOException
- if the socket is already down or an exception occurs
attempting to flush the data to the socket.public void close()
InputStream
and the OutputStream
will be closed.
The socket will be noted as being down.
close
in class Socket
public InputStream getInputStream()
InputStream
.getInputStream
in class Socket
InputStream
public OutputStream getOutputStream()
OutputStream
.getOutputStream
in class Socket
OutputStream
public InetAddress getInetAddress()
InetAddress
.getInetAddress
in class Socket
public InetAddress getLocalAddress()
InetAddress
.getLocalAddress
in class Socket
public int getLocalPort()
getLocalPort
in class Socket
public int getPort()
getPort
in class Socket
public void setTcpNoDelay(boolean on) throws SocketException
setTcpNoDelay
in class Socket
on
- true
=enable Nagle's algorithmSocketException
- if the socket TCP_NODELAY can not be set.public boolean getTcpNoDelay() throws SocketException
getTcpNoDelay
in class Socket
true
if TCP_NODELAY is enabled.public void setSoLinger(boolean on, int val) throws SocketException
65,535
it will be reduced to 65,535
.setSoLinger
in class Socket
on
- true
indicates the socket should linger on.val
- the linger time in seconds.public int getSoLinger() throws SocketException
-1
implies that the option is disabled.getSoLinger
in class Socket
-1
if the linger option is disabled.SocketException
- if the linger setting can not be obtained.public void setSoTimeout(int timeout) throws SocketException
read()
call on the
InputStream
associated with this Socket will block for only this amount of time.
If the timeout expires, a java.io.InterruptedIOException
is raised, though the
Socket
is still valid.
The option must be enabled prior to entering the blocking operation to have effect.
The timeout must be greater than 0
. A timeout of zero is interpreted as an infinite timeout.setSoTimeout
in class Socket
timeout
- the socket timeout value in milliseconds.SocketException
- if the socket timeout can not be set.public int getSoTimeout() throws SocketException
0
implies that the socket timeout option is disabled.getSoTimeout
in class Socket
SocketException
- if the socket timeout can not be obtained.public String toString()
toString
in class Socket
public static void main(String[] args) throws Exception
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |