public class DatagramSocket extends Object implements Closeable
DatagramPacket. A DatagramSocket object can be used for both
endpoints of a connection for a packet delivery service.DatagramPacket,
DatagramSocketImplFactory| Modifier | Constructor and Description |
|---|---|
|
DatagramSocket()
Constructs a UDP datagram socket which is bound to any available port on
the local host using a wildcard address.
|
protected |
DatagramSocket(DatagramSocketImpl socketImpl)
Constructs a new
DatagramSocket using the specific datagram
socket implementation socketImpl. |
|
DatagramSocket(int aPort)
Constructs a UDP datagram socket which is bound to the specific port
aPort on the local host using a wildcard address. |
|
DatagramSocket(int aPort,
InetAddress addr)
Constructs a UDP datagram socket which is bound to the specific local address
addr on
port aPort. |
|
DatagramSocket(SocketAddress localAddr)
Constructs a new
DatagramSocket bound to the host/port specified
by the SocketAddress localAddr or an unbound DatagramSocket if the SocketAddress is null. |
| Modifier and Type | Method and Description |
|---|---|
void |
bind(SocketAddress localAddr)
Binds this socket to the local address and port specified by
localAddr. |
void |
close()
Closes this UDP datagram socket and all possibly associated channels.
|
void |
connect(InetAddress address,
int port)
Connects this datagram socket to the specific
address and port. |
void |
connect(SocketAddress peer)
Connects this datagram socket to the address and port specified by
peer. |
void |
disconnect()
Disconnects this UDP datagram socket from the remote host.
|
boolean |
getBroadcast()
Gets the state of the socket option
SocketOptions.SO_BROADCAST. |
DatagramChannel |
getChannel()
Returns this socket's
DatagramChannel, if one exists. |
FileDescriptor |
getFileDescriptor$() |
InetAddress |
getInetAddress()
Gets the
InetAddress instance representing the remote address to
which this UDP datagram socket is connected. |
InetAddress |
getLocalAddress()
Returns the local address to which this socket is bound, a wildcard address if this
socket is not yet bound, or
null if this socket is closed. |
int |
getLocalPort()
Gets the local port which this socket is bound to.
|
SocketAddress |
getLocalSocketAddress()
Returns the
SocketAddress this socket is bound to, or null for an unbound or
closed socket. |
int |
getPort()
Gets the remote port which this socket is connected to.
|
int |
getReceiveBufferSize()
Returns this socket's
receive buffer size. |
SocketAddress |
getRemoteSocketAddress()
Returns the
SocketAddress this socket is connected to, or null for an unconnected
socket. |
boolean |
getReuseAddress()
Gets the state of the socket option
SocketOptions.SO_REUSEADDR. |
boolean |
getReusePort()
Gets the state of the socket option
SocketOptions.SO_REUSEPORT. |
int |
getSendBufferSize()
Returns this socket's
send buffer size. |
int |
getSoTimeout()
Gets the socket
receive timeout. |
int |
getTrafficClass()
Returns this socket's
SocketOptions.IP_TOS setting. |
boolean |
isBound()
Returns true if this socket is bound to a local address.
|
boolean |
isClosed()
Gets the state of this socket.
|
boolean |
isConnected()
Returns true if this datagram socket is connected to a remote address.
|
void |
onBind(InetAddress localAddress,
int localPort)
Sets the DatagramSocket and its related DatagramSocketImpl state as if a successful bind()
took place, without actually performing an OS bind().
|
void |
onClose()
Sets the DatagramSocket and its related DatagramSocketImpl state as if a successful close()
took place, without actually performing an OS close().
|
void |
onConnect(InetAddress remoteAddress,
int remotePort)
Sets the DatagramSocket and its related DatagramSocketImpl state as if a successful connect()
took place, without actually performing an OS connect().
|
void |
onDisconnect()
Sets the DatagramSocket and its related DatagramSocketImpl state as if a successful
disconnect() took place, without actually performing a disconnect().
|
void |
receive(DatagramPacket pack)
Receives a packet from this socket and stores it in the argument
pack. |
void |
send(DatagramPacket pack)
Sends a packet over this socket.
|
void |
setBroadcast(boolean broadcast)
Sets the socket option
SocketOptions.SO_BROADCAST. |
static void |
setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
Sets the socket implementation factory.
|
void |
setNetworkInterface(NetworkInterface netInterface)
Sets the network interface used by this socket.
|
void |
setReceiveBufferSize(int size)
Sets this socket's
receive buffer size. |
void |
setReuseAddress(boolean reuse)
Sets the socket option
SocketOptions.SO_REUSEADDR. |
void |
setReusePort(boolean reuse)
Sets the socket option
SocketOptions.SO_REUSEPORT. |
void |
setSendBufferSize(int size)
Sets this socket's
send buffer size. |
void |
setSoTimeout(int timeout)
Sets the
read timeout in milliseconds for this socket. |
void |
setTrafficClass(int value)
Sets the
SocketOptions.IP_TOS value for every packet sent by this socket. |
public DatagramSocket()
throws SocketException
SocketException - if an error occurs while creating or binding the socket.public DatagramSocket(int aPort)
throws SocketException
aPort on the local host using a wildcard address. Valid values for aPort are
between 0 and 65535 inclusive.aPort - the port to bind on the local host.SocketException - if an error occurs while creating or binding the socket.public DatagramSocket(int aPort,
InetAddress addr)
throws SocketException
addr on
port aPort. Valid values for aPort are between 0 and 65535 inclusive. If
addr is null the socket will be bound to a wildcard address.aPort - the port to bind on the local host.addr - the address to bind on the local host.SocketException - if an error occurs while creating or binding the socket.protected DatagramSocket(DatagramSocketImpl socketImpl)
DatagramSocket using the specific datagram
socket implementation socketImpl. The created DatagramSocket will not be bound.socketImpl - the DatagramSocketImpl to use.public DatagramSocket(SocketAddress localAddr) throws SocketException
DatagramSocket bound to the host/port specified
by the SocketAddress localAddr or an unbound DatagramSocket if the SocketAddress is null.localAddr - the local machine address and port to bind to.IllegalArgumentException - if the SocketAddress is not supportedSocketException - if a problem occurs creating or binding the socket.public void close()
close in interface Closeableclose in interface AutoCloseablepublic void onClose()
public void disconnect()
public void onDisconnect()
public InetAddress getInetAddress()
InetAddress instance representing the remote address to
which this UDP datagram socket is connected.null if
this socket is not connected.public InetAddress getLocalAddress()
null if this socket is closed.public int getLocalPort()
-1 if this socket is
closed and 0 if it is unbound.public int getPort()
-1
indicates that this socket is not connected.public int getReceiveBufferSize()
throws SocketException
receive buffer size.SocketExceptionpublic int getSendBufferSize()
throws SocketException
send buffer size.SocketExceptionpublic int getSoTimeout()
throws SocketException
receive timeout.SocketException - if an error occurs while getting the option value.public void receive(DatagramPacket pack) throws IOException
pack. All fields of pack must be set according to the data
received. If the received data is longer than the packet buffer size it
is truncated. This method blocks until a packet is received or a timeout
has expired.pack - the DatagramPacket to store the received data.IOException - if an error occurs while receiving the packet.public void send(DatagramPacket pack) throws IOException
pack - the DatagramPacket which has to be sent.IOException - if an error occurs while sending the packet.public void setNetworkInterface(NetworkInterface netInterface) throws SocketException
SocketExceptionpublic void setSendBufferSize(int size)
throws SocketException
send buffer size.SocketExceptionpublic void setReceiveBufferSize(int size)
throws SocketException
receive buffer size.SocketExceptionpublic void setSoTimeout(int timeout)
throws SocketException
read timeout in milliseconds for this socket.
This receive timeout defines the period the socket will block waiting to
receive data before throwing an InterruptedIOException. The value
0 (default) is used to set an infinite timeout. To have effect
this option must be set before the blocking method was called.timeout - the timeout in milliseconds or 0 for no timeout.SocketException - if an error occurs while setting the option.public static void setDatagramSocketImplFactory(DatagramSocketImplFactory fac) throws IOException
fac - the socket factory to use.IOException - if the factory has already been set.DatagramSocketImplFactorypublic void bind(SocketAddress localAddr) throws SocketException
localAddr. If this value is null any free port on a valid local
address is used.localAddr - the local machine address and port to bind on.IllegalArgumentException - if the SocketAddress is not supportedSocketException - if the socket is already bound or a problem occurs during
binding.public void onBind(InetAddress localAddress, int localPort)
public void connect(SocketAddress peer) throws SocketException
peer.
Future calls to send(java.net.DatagramPacket) will use this as the default target, and receive(java.net.DatagramPacket)
will only accept packets from this source.SocketException - if an error occurs.public void onConnect(InetAddress remoteAddress, int remotePort)
public void connect(InetAddress address, int port)
address and port.
Future calls to send(java.net.DatagramPacket) will use this as the default target, and receive(java.net.DatagramPacket)
will only accept packets from this source.
Beware: because it can't throw, this method silently ignores failures.
Use connect(SocketAddress) instead.
public boolean isBound()
bind(java.net.SocketAddress).public boolean isConnected()
connect(java.net.SocketAddress).public SocketAddress getRemoteSocketAddress()
SocketAddress this socket is connected to, or null for an unconnected
socket.public SocketAddress getLocalSocketAddress()
SocketAddress this socket is bound to, or null for an unbound or
closed socket.public void setReuseAddress(boolean reuse)
throws SocketException
SocketOptions.SO_REUSEADDR. This option
has to be enabled if more than one UDP socket wants to be bound to the
same address. That could be needed for receiving multicast packets.
There is an undefined behavior if this option is set after the socket is already bound.
reuse - the socket option value to enable or disable this option.SocketException - if the socket is closed or the option could not be set.public boolean getReuseAddress()
throws SocketException
SocketOptions.SO_REUSEADDR.true if the option is enabled, false otherwise.SocketException - if the socket is closed or the option is invalid.public void setReusePort(boolean reuse)
throws SocketException
SocketOptions.SO_REUSEPORT. This option
has to be enabled if more than one UDP socket wants to be bound to the
same address. That could be needed for receiving multicast packets.
There is an undefined behavior if this option is set after the socket is already bound.
reuse - the socket option value to enable or disable this option.SocketException - if the socket is closed or the option could not be set.public boolean getReusePort()
throws SocketException
SocketOptions.SO_REUSEPORT.true if the option is enabled, false otherwise.SocketException - if the socket is closed or the option is invalid.public void setBroadcast(boolean broadcast)
throws SocketException
SocketOptions.SO_BROADCAST. This option
must be enabled to send broadcast messages.broadcast - the socket option value to enable or disable this option.SocketException - if the socket is closed or the option could not be set.public boolean getBroadcast()
throws SocketException
SocketOptions.SO_BROADCAST.true if the option is enabled, false otherwise.SocketException - if the socket is closed or the option is invalid.public void setTrafficClass(int value)
throws SocketException
SocketOptions.IP_TOS value for every packet sent by this socket.SocketException - if the socket is closed or the option could not be set.public int getTrafficClass()
throws SocketException
SocketOptions.IP_TOS setting.SocketException - if the socket is closed or the option is invalid.public boolean isClosed()
true if the socket is closed, false otherwise.public DatagramChannel getChannel()
DatagramChannel, if one exists. A channel is
available only if this socket wraps a channel. (That is, you can go from a
channel to a socket and back again, but you can't go from an arbitrary socket to a channel.)
In practice, this means that the socket must have been created by
DatagramChannel.open().public final FileDescriptor getFileDescriptor$()