public class DelegatingDatagramSocket extends DatagramSocket
Modifier and Type | Field and Description |
---|---|
private boolean |
closed
Whether this socket has been closed.
|
private static int |
defaultReceiveBufferSize
The size to which to set the receive buffer size.
|
protected DatagramSocket |
delegate
The DatagramSocket to which this
DelegatingDatagramSocket delegates its calls.
|
private static DatagramSocketFactory |
delegateFactory
Assigns a factory to generate custom DatagramSocket to replace classical
"java" DatagramSocket.
|
private long |
nbReceivedPackets
The number of non-STUN packets received on this socket.
|
private long |
nbSentPackets
The number of non-STUN packets sent through this socket.
|
Constructor and Description |
---|
DelegatingDatagramSocket()
Initializes a new DelegatingDatagramSocket instance and binds it
to any available port on the local host machine.
|
DelegatingDatagramSocket(DatagramSocket delegate)
Initializes a new DelegatingDatagramSocket instance which to
implement the DatagramSocket functionality by delegating to a
specific DatagramSocket.
|
DelegatingDatagramSocket(DatagramSocket delegate,
SocketAddress address)
Initializes a new DelegatingDatagramSocket instance which
implements the DatagramSocket functionality.
|
DelegatingDatagramSocket(int port)
Initializes a new DelegatingDatagramSocket instance and binds
it to the specified port on the local host machine.
|
DelegatingDatagramSocket(int port,
InetAddress laddr)
Initializes a new DelegatingDatagramSocket instance bound to the
specified local address.
|
DelegatingDatagramSocket(SocketAddress bindaddr)
Creates a datagram socket, bound to the specified local socket address.
|
Modifier and Type | Method and Description |
---|---|
void |
bind(SocketAddress addr)
Binds this DatagramSocket to a specific address and port.
|
void |
close()
Closes this datagram socket.
|
void |
connect(InetAddress address,
int port)
Connects the socket to a remote address for this socket.
|
void |
connect(SocketAddress addr)
Connects this socket to a remote socket address.
|
void |
disconnect()
Disconnects the socket.
|
boolean |
getBroadcast()
Tests if SO_BROADCAST is enabled.
|
DatagramChannel |
getChannel()
Returns the unique
DatagramChannel object associated with this
datagram socket, if any. |
InetAddress |
getInetAddress()
Returns the address to which this socket is connected.
|
InetAddress |
getLocalAddress()
Gets the local address to which the socket is bound.
|
int |
getLocalPort()
Returns the port number on the local host 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.
|
int |
getPort()
Returns the port for this socket.
|
int |
getReceiveBufferSize()
Gets the value of the SO_RCVBUF option for this
DatagramSocket, that is the buffer size used by the platform for
input on this DatagramSocket.
|
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()
Gets the value of the SO_SNDBUF option for this
DatagramSocket, that is the buffer size used by the platform for
output on this DatagramSocket.
|
int |
getSoTimeout()
Retrieves setting for SO_TIMEOUT.
|
int |
getTrafficClass()
Gets the traffic class or type-of-service in the IP datagram header for
packets sent from this DatagramSocket.
|
private void |
initReceiveBufferSize()
A utility method used by the constructors to ensure the receive buffer
size is set to the preferred default.
|
boolean |
isBound()
Returns the binding state of the socket.
|
boolean |
isClosed()
Returns whether the socket is closed or not.
|
boolean |
isConnected()
Returns the connection state of the socket.
|
(package private) static boolean |
logNonStun(long numOfPacket)
Determines whether a packet should be logged, given the number of sent
or received packets.
|
void |
receive(DatagramPacket p)
Receives a datagram packet from this socket.
|
void |
send(DatagramPacket p)
Sends a datagram packet from this socket.
|
void |
setBroadcast(boolean on)
Enables/disables SO_BROADCAST.
|
static void |
setDefaultDelegateFactory(DatagramSocketFactory factory)
If a factory is provided, then any new instances of
DelegatingDatagramSocket will automatically use a socket from the
factory instead of the super class.
|
static void |
setDefaultReceiveBufferSize(int size)
Specifies a default receive buffer size for the underlying sockets.
|
void |
setReceiveBufferSize(int size)
Sets the SO_RCVBUF option to the specified value for this
DatagramSocket.
|
void |
setReuseAddress(boolean on)
Enables/disables the SO_REUSEADDR socket option.
|
void |
setSendBufferSize(int size)
Sets the SO_SNDBUF option to the specified value for this
DatagramSocket.
|
void |
setSoTimeout(int timeout)
Enables/disables SO_TIMEOUT with the specified timeout, in
milliseconds.
|
void |
setTrafficClass(int tc)
Sets traffic class or type-of-service octet in the IP datagram header for
datagrams sent from this DatagramSocket.
|
setDatagramSocketImplFactory
private static DatagramSocketFactory delegateFactory
private static int defaultReceiveBufferSize
protected final DatagramSocket delegate
private long nbReceivedPackets
private long nbSentPackets
private boolean closed
public DelegatingDatagramSocket() throws SocketException
SocketException
- if the socket could not be opened, or the socket
could not bind to the specified local portDatagramSocket.DatagramSocket()
public DelegatingDatagramSocket(DatagramSocket delegate) throws SocketException
delegate
- the DatagramSocket to which the new instance is
to delegateSocketException
- if anything goes wrong while initializing the new
DelegatingDatagramSocket instancepublic DelegatingDatagramSocket(int port) throws SocketException
port
- the port to bind the new socket toSocketException
- if the socket could not be opened, or the socket
could not bind to the specified local portDatagramSocket.DatagramSocket(int)
public DelegatingDatagramSocket(int port, InetAddress laddr) throws SocketException
port
- the local port to bind the new socket toladdr
- the local address to bind the new socket toSocketException
- if the socket could not be opened, or the socket
could not bind to the specified local portDatagramSocket.DatagramSocket(int, InetAddress)
public DelegatingDatagramSocket(SocketAddress bindaddr) throws SocketException
If, if the address is null, creates an unbound socket.
bindaddr
- local socket address to bind, or null for an
unbound socketSocketException
- if the socket could not be opened, or the socket
could not bind to the specified local portpublic DelegatingDatagramSocket(DatagramSocket delegate, SocketAddress address) throws SocketException
delegate
- the DatagramSocket to which the new instance is
to delegate.address
- The local socket address to bind, or null for an
unbound socket.SocketException
- if the socket could not be opened, or the socket
could not bind to the specified local port.public static void setDefaultDelegateFactory(DatagramSocketFactory factory)
factory
- The factory assigned to generates the new DatagramSocket
for each new DelegatingDatagramSocket which do not use a delegate socket.public static void setDefaultReceiveBufferSize(int size)
size
- the size to which to set the receive buffer size. This value
must be greater than 0.for a discussion of the
effect of changing this setting.
static boolean logNonStun(long numOfPacket)
numOfPacket
- the number of packets sent or received.public void bind(SocketAddress addr) throws SocketException
If the address is null, then the system will pick up an ephemeral port and a valid local address to bind the socket.
bind
in class DatagramSocket
addr
- the address and port to bind toSocketException
- if any error happens during the bind, or if the
socket is already boundSecurityException
- if a security manager exists and its
checkListen method doesn't allow the operationIllegalArgumentException
- if addr is a
SocketAddress subclass not supported by this socketDatagramSocket.bind(SocketAddress)
public void close()
Any thread currently blocked in receive(DatagramPacket)
upon
this socket will throw a SocketException
.
close
in interface Closeable
close
in interface AutoCloseable
close
in class DatagramSocket
DatagramSocket.close()
public void connect(InetAddress address, int port)
If the remote destination to which the socket is connected does not
exist, or is otherwise unreachable, and if an ICMP destination
unreachable packet has been received for that address, then a subsequent
call to send(DatagramPacket)
or receive(DatagramPacket)
may throw a PortUnreachableException. Note, there is no
guarantee that the exception will be thrown.
connect
in class DatagramSocket
address
- the remote address for the socketport
- the remote port for the socketIllegalArgumentException
- if the address is null, or the
port is out of rangeSecurityException
- if the caller is not allowed to send datagrams
to and receive datagrams from the address and portDatagramSocket.connect(InetAddress, int)
public void connect(SocketAddress addr) throws SocketException
connect
in class DatagramSocket
addr
- the remote addressSocketException
- if the connect failsIllegalArgumentException
- if addr is null or a
SocketAddress subclass not supported by this socketDatagramSocket.connect(SocketAddress)
public void disconnect()
disconnect
in class DatagramSocket
DatagramSocket.disconnect()
public boolean getBroadcast() throws SocketException
getBroadcast
in class DatagramSocket
SocketException
- if there is an error in the underlying protocol,
such as an UDP errorDatagramSocket.getBroadcast()
public DatagramChannel getChannel()
DatagramChannel
object associated with this
datagram socket, if any.
A datagram socket will have a channel if, and only if, the channel itself
was created via the DatagramChannel.open()
method
getChannel
in class DatagramSocket
DatagramSocket.getChannel()
public InetAddress getInetAddress()
getInetAddress
in class DatagramSocket
DatagramSocket.getInetAddress()
public InetAddress getLocalAddress()
If there is a security manager, its checkConnect method is first called with the host address and -1 as its arguments to see if the operation is allowed.
getLocalAddress
in class DatagramSocket
DatagramSocket.getLocalAddress()
public int getLocalPort()
getLocalPort
in class DatagramSocket
DatagramSocket.getLocalPort()
public SocketAddress getLocalSocketAddress()
getLocalSocketAddress
in class DatagramSocket
DatagramSocket.getLocalSocketAddress()
public int getPort()
getPort
in class DatagramSocket
DatagramSocket.getPort()
public int getReceiveBufferSize() throws SocketException
getReceiveBufferSize
in class DatagramSocket
SocketException
- if there is an error in the underlying protocol,
such as an UDP errorDatagramSocket.getReceiveBufferSize()
public SocketAddress getRemoteSocketAddress()
getRemoteSocketAddress
in class DatagramSocket
DatagramSocket.getRemoteSocketAddress()
public boolean getReuseAddress() throws SocketException
getReuseAddress
in class DatagramSocket
SocketException
- if there is an error in the underlying protocol,
such as an UDP errorDatagramSocket.getReuseAddress()
public int getSendBufferSize() throws SocketException
getSendBufferSize
in class DatagramSocket
SocketException
- if there is an error in the underlying protocol,
such as an UDP errorDatagramSocket.getSendBufferSize()
public int getSoTimeout() throws SocketException
getSoTimeout
in class DatagramSocket
SocketException
- if there is an error in the underlying protocol,
such as an UDP errorDatagramSocket.getSoTimeout()
public int getTrafficClass() throws SocketException
As the underlying network implementation may ignore the traffic class or
type-of-service set using setTrafficClass(int)
this method may
return a different value than was previously set using the
setTrafficClass(int)
method on this DatagramSocket.
getTrafficClass
in class DatagramSocket
SocketException
- if there is an error obtaining the traffic class
or type-of-service valueDatagramSocket.getTrafficClass()
public boolean isBound()
isBound
in class DatagramSocket
DatagramSocket.isBound()
public boolean isClosed()
isClosed
in class DatagramSocket
DatagramSocket.isClosed()
public boolean isConnected()
isConnected
in class DatagramSocket
DatagramSocket.isConnected()
public void receive(DatagramPacket p) throws IOException
This method blocks until a datagram is received. The length field of the datagram packet object contains the length of the received message. If the message is longer than the packet's length, the message is truncated.
If there is a security manager, a packet cannot be received if the security manager's checkAccept method does not allow it.
receive
in class DatagramSocket
p
- the DatagramPacket into which to place the incoming
dataIOException
- if an I/O error occursDatagramSocket.receive(DatagramPacket)
public void send(DatagramPacket p) throws IOException
If there is a security manager, and the socket is not currently connected to a remote address, this method first performs some security checks. First, if p.getAddress().isMulticastAddress() is true, this method calls the security manager's checkMulticast method with p.getAddress() as its argument. If the evaluation of that expression is false, this method instead calls the security manager's checkConnect method with arguments p.getAddress().getHostAddress() and p.getPort(). Each call to a security manager method could result in a SecurityException if the operation is not allowed.
send
in class DatagramSocket
p
- the DatagramPacket to be sentIOException
- if an I/O error occursDatagramSocket.send(DatagramPacket)
public void setBroadcast(boolean on) throws SocketException
setBroadcast
in class DatagramSocket
on
- whether or not to have broadcast turned onSocketException
- if there is an error in the underlying protocol,
such as an UDP errorDatagramSocket.setBroadcast(boolean)
public void setReceiveBufferSize(int size) throws SocketException
Because SO_RCVBUF is a hint, applications that want to verify
what size the buffers were set to should call
getReceiveBufferSize()
.
setReceiveBufferSize
in class DatagramSocket
size
- the size to which to set the receive buffer size. The value
must be greater than zeroSocketException
- if there is an error in the underlying protocol,
such as an UDP errorIllegalArgumentException
- if the value is zero or is negativeDatagramSocket.setReceiveBufferSize(int)
public void setReuseAddress(boolean on) throws SocketException
setReuseAddress
in class DatagramSocket
on
- whether to enable or disable the SO_REUSEADDR socket
optionSocketException
- if an error occurs enabling or disabling the
SO_RESUEADDR socket option, or the socket is closedDatagramSocket.setReuseAddress(boolean)
public void setSendBufferSize(int size) throws SocketException
As SO_SNDBUF is a hint, applications that want to verify what
size the buffer is should call getSendBufferSize()
.
Increasing the buffer size may allow multiple outgoing packets to be queued by the network implementation when the send rate is high.
setSendBufferSize
in class DatagramSocket
size
- the size to which to set the send buffer size. The value must
be greater than zeroSocketException
- if there is an error in the underlying protocol,
such as an UDP errorIllegalArgumentException
- if the value is zero or is negativeDatagramSocket.setSendBufferSize(int)
public void setSoTimeout(int timeout) throws SocketException
receive(DatagramPacket)
for this DatagramSocket will
block for only this amount of time. If the timeout expires, a
SocketTimeoutException is raised, though the
DatagramSocket is still valid. The option must be enabled prior
to entering the blocking operation to have effect. The timeout must be
greater than zero. A timeout of zero is interpreted as an infinite
timeout.setSoTimeout
in class DatagramSocket
timeout
- the specified timeout in millisecondsSocketException
- if there is an error in the underlying protocol,
such as an UDP errorDatagramSocket.setSoTimeout(int)
public void setTrafficClass(int tc) throws SocketException
setTrafficClass
in class DatagramSocket
tc
- an int value for the bitsetSocketException
- if there is an error setting the traffic class or
type-of-serviceDatagramSocket.setTrafficClass(int)
private void initReceiveBufferSize() throws SocketException
SocketException
- if there is an error in the underlying protocol,
such as an UDP error.Copyright © 2018. All rights reserved.