Constructor and Description |
---|
SocketAdapter(Socket socket)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this socket.
|
void |
doSynchronousSSLHandshake(int timeout)
Performs a "read" on the SSLSocket without removing it from the input stream.
|
InetAddress |
getInetAddress()
Returns the address to which the socket is connected.
|
InputStream |
getInputStream()
Returns an input stream for this socket.
|
InetAddress |
getLocalAddress()
Gets the local address to which the socket is bound.
|
int |
getLocalPort()
Returns the local port number to which this socket is bound.
|
SocketAddress |
getLocalSocketAddress()
Returns the address of the endpoint this socket is bound to, or
null if it is not bound yet. |
OutputStream |
getOutputStream()
Returns an output stream for this socket.
|
int |
getPort()
Returns the remote port number to which this socket is connected.
|
SocketAddress |
getRemoteSocketAddress()
Returns the address of the endpoint this socket is connected to, or
null if it is unconnected. |
int |
getSoTimeout()
Returns setting for SO_TIMEOUT.
|
SSLSocket |
getSSLSocket()
Gets the underlying SSLSocket.
|
boolean |
getTcpNoDelay()
Tests if TCP_NODELAY is enabled.
|
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.
|
boolean |
isSSLSocket()
Checks if this is an SSL socket.
|
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).
|
void |
shutdownInput()
Places the input stream for this socket at "end of stream".
|
void |
shutdownOutput()
Disables the output stream for this socket.
|
public SocketAdapter(Socket socket)
public boolean isSSLSocket()
isSSLSocket
in interface ISocket
public SSLSocket getSSLSocket()
getSSLSocket
in interface ISocket
public InetAddress getInetAddress()
If the socket was connected prior to being closed
,
then this method will continue to return the connected address
after the socket is closed.
getInetAddress
in interface ISocket
null
if the socket is not connected.public InetAddress getLocalAddress()
getLocalAddress
in interface ISocket
wildcard
address
if the socket is closed or not bound yet.public int getPort()
If the socket was connected prior to being closed
,
then this method will continue to return the connected port number
after the socket is closed.
public int getLocalPort()
If the socket was bound prior to being closed
,
then this method will continue to return the local port number
after the socket is closed.
getLocalPort
in interface ISocket
public SocketAddress getRemoteSocketAddress()
null
if it is unconnected.
If the socket was connected prior to being closed
,
then this method will continue to return the connected address
after the socket is closed.
getRemoteSocketAddress
in interface ISocket
SocketAddress
representing the remote endpoint of this
socket, or null
if it is not connected yet.public SocketAddress getLocalSocketAddress()
null
if it is not bound yet.
If a socket bound to an endpoint represented by an
InetSocketAddress
is closed
,
then this method will continue to return an InetSocketAddress
after the socket is closed. In that case the returned
InetSocketAddress
's address is the
wildcard
address
and its port is the local port that it was bound to.
getLocalSocketAddress
in interface ISocket
SocketAddress
representing the local endpoint of this
socket, or null
if it is not bound yet.public InputStream getInputStream() throws 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 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
.
Closing the returned InputStream
will close the associated socket.
getInputStream
in interface ISocket
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 OutputStream getOutputStream() throws 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 IllegalBlockingModeException
.
Closing the returned OutputStream
will close the associated socket.
getOutputStream
in interface ISocket
IOException
- if an I/O error occurs when creating the
output stream or if the socket is not connected.public void setSoTimeout(int timeout) throws SocketException
setSoTimeout
in interface ISocket
timeout
- the specified timeout, in milliseconds.SocketException
- if there is an error
in the underlying protocol, such as a TCP error.public int getSoTimeout() throws SocketException
getSoTimeout
in interface ISocket
SocketException
- if there is an error
in the underlying protocol, such as a TCP error.public void setTcpNoDelay(boolean on) throws SocketException
setTcpNoDelay
in interface ISocket
on
- true
to enable TCP_NODELAY,
false
to disable.SocketException
- if there is an error
in the underlying protocol, such as a TCP error.ISocket.getTcpNoDelay()
public boolean getTcpNoDelay() throws SocketException
getTcpNoDelay
in interface ISocket
boolean
indicating whether or not TCP_NODELAY is enabled.SocketException
- if there is an error
in the underlying protocol, such as a TCP error.public void close() throws IOException
Any thread currently blocked in an I/O operation upon this socket
will throw a 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.
Closing this socket will also close the socket's
InputStream
and
OutputStream
.
If this socket has an associated channel then the channel is closed as well.
close
in interface ISocket
IOException
- if an I/O error occurs when closing this socket.public void shutdownInput() throws IOException
If you read from a socket input stream after invoking shutdownInput() on the socket, the stream will return EOF.
shutdownInput
in interface ISocket
IOException
- if an I/O error occurs when shutting down this
socket.public void shutdownOutput() throws IOException
If you write to a socket output stream after invoking shutdownOutput() on the socket, the stream will throw an IOException.
shutdownOutput
in interface ISocket
IOException
- if an I/O error occurs when shutting down this socket.public boolean isConnected()
Note: Closing a socket doesn't clear its connection state, which means
this method will return true
for a closed socket
(see isClosed()
) if it was successfully connected prior
to being closed.
isConnected
in interface ISocket
public boolean isBound()
Note: Closing a socket doesn't clear its binding state, which means
this method will return true
for a closed socket
(see isClosed()
) if it was successfully bound prior
to being closed.
public boolean isClosed()
public boolean isInputShutdown()
isInputShutdown
in interface ISocket
public boolean isOutputShutdown()
isOutputShutdown
in interface ISocket
public void doSynchronousSSLHandshake(int timeout) throws IOException
doSynchronousSSLHandshake
in interface ISocket
timeout
- Time in milliseconds to complete the handshake.IOException
- In case of socket errors.Phantom® and NetPhantom® are registered trademarks of Mindus SARL.
© © Mindus SARL, 2024. All rights reserved.