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 UDP datagram socket to the specific target host with the
address
anAdress on port aPort . | |||||||||||
Connects this datagram socket to the remote host and port specified by
remoteAddr . | |||||||||||
Disconnects this UDP datagram socket from the remote host.
| |||||||||||
Gets the state of the socket option
SocketOptions.SO_BROADCAST . | |||||||||||
Gets the related DatagramChannel of this socket.
| |||||||||||
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.
| |||||||||||
Gets the socket receive buffer size.
| |||||||||||
Gets the address and port of the connected remote host.
| |||||||||||
Gets the state of the socket option
SocketOptions.SO_REUSEADDR . | |||||||||||
Gets the socket send buffer size.
| |||||||||||
Gets the socket receive timeout in milliseconds.
| |||||||||||
Gets the value of the type-of-service socket option
SocketOptions.IP_TOS . | |||||||||||
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 the socket receive buffer size.
| |||||||||||
Sets the socket option
SocketOptions.SO_REUSEADDR . | |||||||||||
Sets the socket send buffer size.
| |||||||||||
Sets the timeout period in milliseconds for the
receive() method. | |||||||||||
Sets the socket option
SocketOptions.IP_TOS . |
[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 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. |
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. |
---|
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. |
---|
Gets the related DatagramChannel of this socket. This implementation
returns always null
.
null
if this socket was
not created by a DatagramChannel
object.
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.
Gets the socket receive buffer size. ( SocketOptions.SO_RCVBUF
)
SocketException | if an error occurs while getting the option value. |
---|
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. |
---|
Gets the socket send buffer size. ( SocketOptions.SO_SNDBUF
)
SocketException | if an error occurs while getting the option value. |
---|
Gets the socket receive timeout in milliseconds. The return value 0
implies the timeout is disabled/infinitive. ( SocketOptions.SO_TIMEOUT
)
SocketException | if an error occurs while getting the option value. |
---|
Gets the value of the type-of-service socket option SocketOptions.IP_TOS
.
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 the socket receive buffer size. This buffer size determines which
the maximum packet size is that can be received over this socket. It
depends on the network implementation what will happen if the packet is
bigger than the buffer size. ( SocketOptions.SO_RCVBUF
)
size | the buffer size in bytes. The size must be at least one byte. |
---|
SocketException | if an error occurs while setting the option. |
---|
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 the socket send buffer size. This buffer size determines which the
maximum packet size is that can be sent over this socket. It depends on
the network implementation what will happen if the packet is bigger than
the buffer size. ( SocketOptions.SO_SNDBUF
)
size | the buffer size in bytes. The size must be at least one byte. |
---|
SocketException | if an error occurs while setting the option. |
---|
Sets the timeout period in milliseconds for the receive()
method.
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. ( SocketOptions.SO_TIMEOUT
)
timeout | the timeout period in milliseconds or 0 for infinite. |
---|
SocketException | if an error occurs while setting the option. |
---|
Sets the socket option SocketOptions.IP_TOS
. This option defines
the value of the type-of-service field of the IP-header for every packet
sent by this socket. The value could be ignored by the underlying network
implementation.
Values between 0
and 255
inclusive are valid for this
option.
value | the socket option value to be set as type-of-service. |
---|
SocketException | if the socket is closed or the option could not be set. |
---|