public class SafeCloseDatagramSocket extends DelegatingDatagramSocket
DatagramSocket.close()
is called while another thread is blocked in
its DatagramSocket.receive(DatagramPacket)
, calling
DatagramSocket.bind(SocketAddress)
throws an "Already bound"
exception until (the native counterpart of) the receive method
returns.Modifier and Type | Field and Description |
---|---|
private int |
inReceive
The number of
receive(DatagramPacket) calls that have to return
before close() returns. |
private Object |
inReceiveSyncRoot
The Object which synchronizes the access to
inReceive
and implements the related inter-thread communication. |
delegate
Constructor and Description |
---|
SafeCloseDatagramSocket()
Initializes a new SafeCloseDatagramSocket instance and binds it
to any available port on the local host machine.
|
SafeCloseDatagramSocket(DatagramSocket delegate)
Initializes a new SafeCloseDatagramSocket instance which to
implement the DatagramSocket functionality by delegating to a
specific DatagramSocket.
|
SafeCloseDatagramSocket(int port)
Initializes a new SafeCloseDatagramSocket instance and binds it
to the specified port on the local host machine.
|
SafeCloseDatagramSocket(int port,
InetAddress laddr)
Initializes a new SafeCloseDatagramSocket instance bound to the
specified local address.
|
SafeCloseDatagramSocket(SocketAddress bindaddr)
Creates a datagram socket, bound to the specified local socket address.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this datagram socket.
|
void |
receive(DatagramPacket p)
Receives a datagram packet from this socket.
|
bind, connect, connect, disconnect, getBroadcast, getChannel, getInetAddress, getLocalAddress, getLocalPort, getLocalSocketAddress, getPort, getReceiveBufferSize, getRemoteSocketAddress, getReuseAddress, getSendBufferSize, getSoTimeout, getTrafficClass, isBound, isClosed, isConnected, logNonStun, send, setBroadcast, setDefaultDelegateFactory, setDefaultReceiveBufferSize, setReceiveBufferSize, setReuseAddress, setSendBufferSize, setSoTimeout, setTrafficClass
setDatagramSocketImplFactory
private int inReceive
receive(DatagramPacket)
calls that have to return
before close()
returns.public SafeCloseDatagramSocket() throws SocketException
SocketException
- if the socket could not be opened, or the socket
could not bind to the specified local portDatagramSocket.DatagramSocket()
public SafeCloseDatagramSocket(DatagramSocket delegate) throws SocketException
delegate
- the DatagramSocket to which the new instance is
to delegateSocketException
- if anything goes wrong while initializing the new
SafeCloseDatagramSocket instancepublic SafeCloseDatagramSocket(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 SafeCloseDatagramSocket(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 SafeCloseDatagramSocket(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 void close()
Any thread currently blocked in receive(java.net.DatagramPacket)
upon this socket will
throw a SocketException
and this datagram socket will wait for it
to return.
close
in interface Closeable
close
in interface AutoCloseable
close
in class DelegatingDatagramSocket
DatagramSocket.close()
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.
receive
in class DelegatingDatagramSocket
p
- the DatagramPacket into which to place the incoming
dataIOException
- if an I/O error occursDatagramSocket.receive(DatagramPacket)
Copyright © 2018. All rights reserved.