public abstract class DatagramSocketClient extends Object implements AutoCloseable
setDatagramSocketFactory) method, which allows you to control the type of DatagramSocket the DatagramSocketClient creates
for network communications. This is especially useful for adding things like proxy support as well as better support for applets. For example, you could
create a DatagramSocketFactory that requests browser security capabilities before creating a socket. All classes derived from
DatagramSocketClient should use the _socketFactory_ member variable to create DatagramSocket instances rather than instantiating
them by directly invoking a constructor. By honoring this contract you guarantee that a user will always be able to provide his own Socket implementations by
substituting his own SocketFactory.DatagramSocketFactory| Modifier and Type | Field and Description |
|---|---|
protected boolean |
_isOpen_
A status variable indicating if the client's socket is currently open.
|
protected DatagramSocket |
_socket_
The datagram socket used for the connection.
|
protected DatagramSocketFactory |
_socketFactory_
The datagram socket's DatagramSocketFactory.
|
protected int |
_timeout_
The timeout to use after opening a socket.
|
| Constructor and Description |
|---|
DatagramSocketClient()
Constructs a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
protected DatagramSocket |
checkOpen()
Gets the non-null DatagramSocket or throws
NullPointerException. |
void |
close()
Closes the DatagramSocket used for the connection.
|
Charset |
getCharset()
Gets the charset.
|
int |
getDefaultTimeout()
Gets the default timeout in milliseconds that is used when opening a socket.
|
InetAddress |
getLocalAddress()
Gets the local address to which the client's socket is bound.
|
int |
getLocalPort()
Gets the port number of the open socket on the local host used for the connection.
|
java.time.Duration |
getSoTimeoutDuration()
Gets the timeout duration of the currently opened socket.
|
boolean |
isOpen()
Tests whether the client has a currently open socket.
|
void |
open()
Opens a DatagramSocket on the local host at the first available port.
|
void |
open(int port)
Opens a DatagramSocket on the local host at a specified port.
|
void |
open(int port,
InetAddress localAddress)
Opens a DatagramSocket at the specified address on the local host at a specified port.
|
void |
setCharset(Charset charset)
Sets the charset.
|
void |
setDatagramSocketFactory(DatagramSocketFactory factory)
Sets the DatagramSocketFactory used by the DatagramSocketClient to open DatagramSockets.
|
void |
setDefaultTimeout(java.time.Duration timeout)
Sets the default timeout in to use when opening a socket.
|
void |
setSoTimeout(java.time.Duration timeout)
Sets the timeout duration of a currently open connection.
|
protected boolean _isOpen_
protected DatagramSocket _socket_
protected DatagramSocketFactory _socketFactory_
protected int _timeout_
public DatagramSocketClient()
protected DatagramSocket checkOpen()
NullPointerException.
This method does not allocate resources.
public void close()
open() again. _isOpen_ is set to false and _socket_ is set to null.close in interface AutoCloseablepublic Charset getCharset()
public int getDefaultTimeout()
public InetAddress getLocalAddress()
public int getLocalPort()
public java.time.Duration getSoTimeoutDuration()
throws SocketException
SocketException - if an error getting the timeout.public boolean isOpen()
public void open()
throws SocketException
setDefaultTimeout().
_isOpen_ is set to true after calling this method and _socket_ is set to the newly opened socket.
SocketException - If the socket could not be opened or the timeout could not be set.public void open(int port)
throws SocketException
setDefaultTimeout().
_isOpen_ is set to true after calling this method and _socket_ is set to the newly opened socket.
port - The port to use for the socket.SocketException - If the socket could not be opened or the timeout could not be set.public void open(int port,
InetAddress localAddress)
throws SocketException
setDefaultTimeout().
_isOpen_ is set to true after calling this method and _socket_ is set to the newly opened socket.
port - The port to use for the socket.localAddress - The local address to use.SocketException - If the socket could not be opened or the timeout could not be set.public void setCharset(Charset charset)
charset - the charset.public void setDatagramSocketFactory(DatagramSocketFactory factory)
factory - The new DatagramSocketFactory the DatagramSocketClient should use.public void setDefaultTimeout(java.time.Duration timeout)
open() and should not be confused with setSoTimeout() which operates on the
currently open socket. _timeout_ contains the new timeout value.timeout - The timeout durations to use for the datagram socket connection.public void setSoTimeout(java.time.Duration timeout)
throws SocketException
open().timeout - The timeout in milliseconds to use for the currently open datagram socket connection.SocketException - if an error setting the timeout.