java.lang.Object | |
↳ | java.net.DatagramSocket |
Known Direct Subclasses |
This class implements a UDP socket for sending and receiving DatagramPacket
. A DatagramSocket
object can be used for both
endpoints of a connection for a packet delivery service.
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a UDP datagram socket which is bound to any available port on
the local host using a wildcard address.
| |||||||||||
Constructs a UDP datagram socket which is bound to the specific port
aPort on the local host using a wildcard address.
| |||||||||||
Constructs a UDP datagram socket which is bound to the specific local address
addr on
port aPort .
| |||||||||||
Constructs a new
DatagramSocket bound to the host/port specified
by the SocketAddress localAddr or an unbound DatagramSocket if the SocketAddress is null .
|
Protected Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new
DatagramSocket using the specific datagram
socket implementation socketImpl .
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Binds this socket to the local address and port specified by
localAddr .
| |||||||||||
Closes this UDP datagram socket and all possibly associated channels.
| |||||||||||
Connects this datagram socket to the address and port specified by
peer .
| |||||||||||
Connects this datagram socket to the specific
address and port .
| |||||||||||
Disconnects this UDP datagram socket from the remote host.
| |||||||||||
Gets the state of the socket option
SocketOptions.SO_BROADCAST .
| |||||||||||
Returns this socket's
DatagramChannel , if one exists.
| |||||||||||
Gets the
InetAddress instance representing the remote address to
which this UDP datagram socket is connected.
| |||||||||||
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.
| |||||||||||
Gets the local port which this socket is bound to.
| |||||||||||
Returns the
SocketAddress this socket is bound to, or null for an unbound or
closed socket.
| |||||||||||
Gets the remote port which this socket is connected to.
| |||||||||||
Returns this socket's
receive buffer size .
| |||||||||||
Returns the
SocketAddress this socket is connected to, or null for an unconnected
socket.
| |||||||||||
Gets the state of the socket option
SocketOptions.SO_REUSEADDR .
| |||||||||||
Returns this socket's
send buffer size .
| |||||||||||
Gets the socket
receive timeout .
| |||||||||||
Returns this socket's setting.
| |||||||||||
Returns true if this socket is bound to a local address.
| |||||||||||
Gets the state of this socket.
| |||||||||||
Returns true if this datagram socket is connected to a remote address.
| |||||||||||
Receives a packet from this socket and stores it in the argument
pack .
| |||||||||||
Sends a packet over this socket.
| |||||||||||
Sets the socket option
SocketOptions.SO_BROADCAST .
| |||||||||||
Sets the socket implementation factory.
| |||||||||||
Sets this socket's
receive buffer size .
| |||||||||||
Sets the socket option
SocketOptions.SO_REUSEADDR .
| |||||||||||
Sets this socket's
send buffer size .
| |||||||||||
Sets the
read timeout in milliseconds for this socket.
| |||||||||||
Sets the value for every packet sent by this socket.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
java.io.Closeable
| |||||||||||
From interface
java.lang.AutoCloseable
|
Constructs a UDP datagram socket which is bound to any available port on the local host using a wildcard address.
SocketException | if an error occurs while creating or binding the socket. |
---|
Constructs a UDP datagram socket which is bound to the specific port
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. |
---|
Constructs a UDP datagram socket which is bound to the specific local address 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. |
---|
Constructs a new 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 supported |
---|---|
SocketException | if a problem occurs creating or binding the socket. |
Constructs a new DatagramSocket
using the specific datagram
socket implementation socketImpl
. The created DatagramSocket
will not be bound.
socketImpl | the DatagramSocketImpl to use. |
---|
Binds this socket to the local address and port specified by 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 supported |
---|---|
SocketException | if the socket is already bound or a problem occurs during binding. |
Closes this UDP datagram socket and all possibly associated channels.
Connects this datagram socket to the address and port specified by peer
.
Future calls to send(DatagramPacket)
will use this as the default target, and receive(DatagramPacket)
will only accept packets from this source.
SocketException | if an error occurs. |
---|
Connects this datagram socket to the specific address
and port
.
Future calls to send(DatagramPacket)
will use this as the default target, and receive(DatagramPacket)
will only accept packets from this source.
Beware: because it can't throw, this method silently ignores failures.
Use connect(SocketAddress)
instead.
Disconnects this UDP datagram socket from the remote host. This method called on an unconnected socket does nothing.
Gets the state of the socket option SocketOptions.SO_BROADCAST
.
true
if the option is enabled, false
otherwise.SocketException | if the socket is closed or the option is invalid. |
---|
Returns this socket's 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
open()
.
Gets the InetAddress
instance representing the remote address to
which this UDP datagram socket is connected.
null
if
this socket is not connected.
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.
Gets the local port which this socket is bound to.
-1
if this socket is
closed and 0
if it is unbound.
Returns the SocketAddress
this socket is bound to, or null
for an unbound or
closed socket.
Gets the remote port which this socket is connected to.
-1
indicates that this socket is not connected.
Returns this socket's receive buffer size
.
SocketException |
---|
Returns the SocketAddress
this socket is connected to, or null for an unconnected
socket.
Gets the state of the socket option SocketOptions.SO_REUSEADDR
.
true
if the option is enabled, false
otherwise.SocketException | if the socket is closed or the option is invalid. |
---|
Returns this socket's send buffer size
.
SocketException |
---|
Gets the socket receive timeout
.
SocketException | if an error occurs while getting the option value. |
---|
Returns this socket's setting.
SocketException | if the socket is closed or the option is invalid. |
---|
Returns true if this socket is bound to a local address. See bind(SocketAddress)
.
Gets the state of this socket.
true
if the socket is closed, false
otherwise.
Returns true if this datagram socket is connected to a remote address. See connect(InetAddress, int)
.
Receives a packet from this socket and stores it in the argument 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. |
---|
Sends a packet over this socket.
pack | the DatagramPacket which has to be sent. |
---|
IOException | if an error occurs while sending the packet. |
---|
Sets the socket option 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. |
---|
Sets the socket implementation factory. This may only be invoked once over the lifetime of the application. This factory is used to create a new datagram socket implementation.
fac | the socket factory to use. |
---|
IOException | if the factory has already been set. |
---|
Sets this socket's receive buffer size
.
SocketException |
---|
Sets the socket option 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. |
---|
Sets this socket's send buffer size
.
SocketException |
---|
Sets the 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. |
---|
Sets the value for every packet sent by this socket.
SocketException | if the socket is closed or the option could not be set. |
---|