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 localhost.
| |||||||||||
Constructs a UDP datagram socket which is bound to the specific port
aPort on the localhost. | |||||||||||
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 remote host and port specified by
remoteAddr . | |||||||||||
Connects this UDP datagram socket to the specific target host with the
address
anAdress on port aPort . | |||||||||||
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. | |||||||||||
Gets the
InetAddress instance representing the bound local
address of this UDP datagram socket. | |||||||||||
Gets the local port which this socket is bound to.
| |||||||||||
Gets the bound local address and port of this socket.
| |||||||||||
Gets the remote port which this socket is connected to.
| |||||||||||
Returns this socket's
receive buffer size . | |||||||||||
Gets the address and port of the connected remote host.
| |||||||||||
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.
| |||||||||||
Determines whether the socket is bound to an address or not.
| |||||||||||
Gets the state of this socket.
| |||||||||||
Determines whether the socket is connected to a target host.
| |||||||||||
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
|
Constructs a UDP datagram socket which is bound to any available port on the localhost.
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 localhost. Valid values for aPort
are
between 0 and 65535 inclusive.
aPort | the port to bind on the localhost. |
---|
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.
aPort | the port to bind on the localhost. |
---|---|
addr | the address to bind on the localhost. |
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 remote host and port specified by
remoteAddr
. The host and port are validated, thereafter the only
validation on send()
and receive()
is that the packet
address/port matches the connected target.
remoteAddr | the address and port of the target host. |
---|
SocketException | if an error occurs during connecting. |
---|
Connects this UDP datagram socket to the specific target host with the
address anAdress
on port aPort
. The host and port are
validated, thereafter the only validation on send()
and receive()
is to check whether the packet address/port matches the
connected target.
anAddress | the target address of this socket. |
---|---|
aPort | the target port of this socket. |
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.
Gets the InetAddress
instance representing the bound local
address of this UDP datagram socket.
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.
Gets the bound local address and port of this socket. If the socket is
unbound, null
is returned.
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 |
---|
Gets the address and port of the connected remote host. If this socket is
not connected yet, null
is returned.
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. |
---|
Determines whether the socket is bound to an address or not.
true
if the socket is bound, false
otherwise.
Gets the state of this socket.
true
if the socket is closed, false
otherwise.
Determines whether the socket is connected to a target host.
true
if the socket is connected, false
otherwise.
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. If a security manager exists, its checkAccept
method
determines whether or not a packet is discarded. Any packets from
unacceptable origins are silently discarded.
pack | the DatagramPacket to store the received data. |
---|
IOException | if an error occurs while receiving the packet. |
---|
Sends a packet over this socket. The packet must satisfy the security policy before it may be sent. If a security manager is installed, this method checks whether it is allowed to send this packet to the specified address.
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. If a security manager is set its
method checkSetFactory()
is called to check if the operation is
allowed. A SecurityException
is thrown if the operation is not
allowed.
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. |
---|