|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
An interface to an underlying socket. This interface has many of the same methods as java.net.Socket, but allows different socket implementations to be plugged in.
| Method Summary | |
void |
bind(java.net.SocketAddress bindpoint)
Binds the socket to a local address. |
void |
close()
Closes this socket. |
void |
connect(java.net.SocketAddress endpoint)
Connects this socket to the server. |
void |
connect(java.net.SocketAddress endpoint,
int timeout)
Connects this socket to the server with a specified timeout value. |
java.io.InputStream |
getInputStream()
Returns an input stream for this socket. |
int |
getLocalPort()
Returns the local port to which this socket is bound. |
java.net.SocketAddress |
getLocalSocketAddress()
Returns the address of the endpoint this socket is bound to, or null if it is not bound yet. |
java.io.OutputStream |
getOutputStream()
Returns an output stream for this socket. |
int |
getPort()
Returns the remote port to which this socket is connected. |
int |
getReceiveBufferSize()
Gets the value of the SO_RCVBUF option for this Socket, that is the buffer size used by the platform for input on this Socket. |
java.net.SocketAddress |
getRemoteSocketAddress()
Returns the address of the endpoint this socket is connected to, or null if it is unconnected. |
boolean |
getReuseAddress()
Tests if SO_REUSEADDR is enabled. |
int |
getSendBufferSize()
Get value of the SO_SNDBUF option for this Socket, that is the buffer size used by the platform for output on this Socket. |
int |
getSoTimeout()
Returns setting for SO_TIMEOUT. |
boolean |
isBound()
Returns the binding state of the socket. |
boolean |
isClosed()
Returns the closed state of the socket. |
boolean |
isConnected()
Returns the connection state of the socket. |
boolean |
isInputShutdown()
Returns whether the read-half of the socket connection is closed. |
boolean |
isOutputShutdown()
Returns whether the write-half of the socket connection is closed. |
void |
setReceiveBufferSize(int size)
Sets the SO_RCVBUF option to the specified value for this Socket. |
void |
setReuseAddress(boolean on)
Enable/disable the SO_REUSEADDR socket option. |
void |
setSendBufferSize(int size)
Sets the SO_SNDBUF option to the specified value for this Socket. |
void |
setSoTimeout(int timeout)
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. |
void |
shutdownInput()
Places the input stream for this socket at "end of stream". |
void |
shutdownOutput()
Disables the output stream for this socket. |
| Method Detail |
public void bind(java.net.SocketAddress bindpoint)
throws java.io.IOException
If the address is null, then the system will pick up
an ephemeral port and a valid local address to bind the socket.
bindpoint - the SocketAddress to bind to
java.lang.IllegalArgumentException - if bindpoint is a
SocketAddress subclass not supported by this socket
java.io.IOException - if the bind operation fails, or if the socket
is already bound.isBound()public boolean isBound()
bind(java.net.SocketAddress)
public void connect(java.net.SocketAddress endpoint)
throws java.io.IOException
endpoint - the SocketAddress
java.nio.channels.IllegalBlockingModeException - if this socket has an associated channel,
and the channel is in non-blocking mode
java.lang.IllegalArgumentException - if endpoint is null or is a
SocketAddress subclass not supported by this socket
java.io.IOException - if an error occurs during the connection
public void connect(java.net.SocketAddress endpoint,
int timeout)
throws java.io.IOException
endpoint - the SocketAddresstimeout - the timeout value to be used in milliseconds.
java.nio.channels.IllegalBlockingModeException - if this socket has an associated channel,
and the channel is in non-blocking mode
java.lang.IllegalArgumentException - if endpoint is null or is a
SocketAddress subclass not supported by this socket
java.io.IOException - if an error occurs during the connection
java.net.SocketTimeoutException - if timeout expires before connectingpublic boolean isConnected()
public void close()
throws java.io.IOException
SocketException.
Once a socket has been closed, it is not available for further networking
use (i.e. can't be reconnected or rebound). A new socket needs to be
created.
If this socket has an associated channel then the channel is closed as well.
java.io.IOException - if an I/O error occurs when closing this socket.isClosed()public boolean isClosed()
close()
public java.io.InputStream getInputStream()
throws java.io.IOException
If this socket has an associated channel then the resulting input
stream delegates all of its operations to the channel. If the channel
is in non-blocking mode then the input stream's read operations
will throw an java.nio.channels.IllegalBlockingModeException.
Under abnormal conditions the underlying connection may be broken by the remote host or the network software (for example a connection reset in the case of TCP connections). When a broken connection is detected by the network software the following applies to the returned input stream :-
The network software may discard bytes that are buffered
by the socket. Bytes that aren't discarded by the network
software can be read using read.
If there are no bytes buffered on the socket, or all
buffered bytes have been consumed by
read, then all subsequent
calls to read will throw an
IOException.
If there are no bytes buffered on the socket, and the
socket has not been closed using close, then
available will
return 0.
java.io.IOException - if an I/O error occurs when creating the
input stream, the socket is closed, the socket is
not connected, or the socket input has been shutdown
using shutdownInput()
public java.io.OutputStream getOutputStream()
throws java.io.IOException
If this socket has an associated channel then the resulting output
stream delegates all of its operations to the channel. If the channel
is in non-blocking mode then the output stream's write
operations will throw an java.nio.channels.IllegalBlockingModeException.
java.io.IOException - if an I/O error occurs when creating the
output stream or if the socket is not connected.public java.net.SocketAddress getLocalSocketAddress()
null if it is not bound yet.
SocketAddress representing the local endpoint of this
socket, or null if it is not bound yet.getLocalPort(),
bind(java.net.SocketAddress)public int getLocalPort()
public int getPort()
public java.net.SocketAddress getRemoteSocketAddress()
null if it is unconnected.
SocketAddress reprensenting the remote endpoint of this
socket, or null if it is not connected yet.getPort(),
connect(java.net.SocketAddress, int),
connect(java.net.SocketAddress)
public int getSoTimeout()
throws java.net.SocketException
java.net.SocketException - if there is an error
in the underlying protocol, such as a TCP error.setSoTimeout(int)
public void setSoTimeout(int timeout)
throws java.net.SocketException
timeout - the specified timeout, in milliseconds.
java.net.SocketException - if there is an error
in the underlying protocol, such as a TCP error.getSoTimeout()
public int getSendBufferSize()
throws java.net.SocketException
java.net.SocketException - if there is an error
in the underlying protocol, such as a TCP error.setSendBufferSize(int)
public void setSendBufferSize(int size)
throws java.net.SocketException
Because SO_SNDBUF is a hint, applications that want to
verify what size the buffers were set to should call
getSendBufferSize().
size - the size to which to set the send buffer
size. This value must be greater than 0.
java.net.SocketException - if there is an error
in the underlying protocol, such as a TCP error.
java.lang.IllegalArgumentException - if the
value is 0 or is negative.getSendBufferSize()
public int getReceiveBufferSize()
throws java.net.SocketException
java.net.SocketException - if there is an error
in the underlying protocol, such as a TCP error.setReceiveBufferSize(int)
public void setReceiveBufferSize(int size)
throws java.net.SocketException
Increasing the receive buffer size can increase the performance of network I/O for high-volume connection, while decreasing it can help reduce the backlog of incoming data.
Because SO_RCVBUF is a hint, applications that want to
verify what size the buffers were set to should call
getReceiveBufferSize().
The value of SO_RCVBUF is also used to set the TCP receive window that is advertized to the remote peer. Generally, the window size can be modified at any time when a socket is connected. However, if a receive window larger than 64K is required then this must be requested before the socket is connected to the remote peer. There are two cases to be aware of:
ServerSocket.setReceiveBufferSize(int) before the ServerSocket
is bound to a local address.
size - the size to which to set the receive buffer
size. This value must be greater than 0.
java.lang.IllegalArgumentException - if the value is 0 or is
negative.
java.net.SocketException - if there is an error
in the underlying protocol, such as a TCP error.getReceiveBufferSize(),
ServerSocket.setReceiveBufferSize(int)
public boolean getReuseAddress()
throws java.net.SocketException
boolean indicating whether or not SO_REUSEADDR is enabled.
java.net.SocketException - if there is an error
in the underlying protocol, such as a TCP error.setReuseAddress(boolean)
public void setReuseAddress(boolean on)
throws java.net.SocketException
bind(java.net.SocketAddress) allows the socket to be
bound even though a previous connection is in a timeout
state.
When a Socket is created the initial setting
of SO_REUSEADDR is disabled.
The behaviour when SO_REUSEADDR is enabled or
disabled after a socket is bound (See isBound())
is not defined.
on - whether to enable or disable the socket option
java.net.SocketException - if an error occurs enabling or
disabling the SO_RESUEADDR socket option,
or the socket is closed.getReuseAddress(),
bind(java.net.SocketAddress),
isClosed(),
isBound()
public void shutdownInput()
throws java.io.IOException
java.io.IOException - if an I/O error occurs when shutting down this
socket.Socket.shutdownOutput(),
Socket.close(),
Socket.setSoLinger(boolean, int),
isInputShutdown()public boolean isInputShutdown()
shutdownInput()
public void shutdownOutput()
throws java.io.IOException
java.io.IOException - if an I/O error occurs when shutting down this
socket.Socket.shutdownInput(),
Socket.close(),
Socket.setSoLinger(boolean, int),
isOutputShutdown()public boolean isOutputShutdown()
shutdownOutput()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||