A datagram socket is the sending or receiving point for a packet delivery service. Each packet sent or received on a datagram socket is individually addressed and routed. Multiple packets sent from one machine to another may be routed differently, and may arrive in any order.
UDP broadcasts sends are always enabled on a DatagramSocket. In order to receive broadcast packets a DatagramSocket should be bound to the wildcard address. In some implementations, broadcast packets may also be received when a DatagramSocket is bound to a more specific address.
Example:
DatagramSocket s = new DatagramSocket(null);
s.bind(new InetSocketAddress(8888));
Which is equivalent to:
DatagramSocket s = new DatagramSocket(8888);
Both cases will create a DatagramSocket able to receive broadcasts on
UDP port 8888.
If there is a security manager,
its checkListen
method is first called
with 0 as its argument to ensure the operation is allowed.
This could result in a SecurityException.
If, if the address is null
, creates an unbound socket.
If there is a security manager,
its checkListen
method is first called
with the port from the socket address
as its argument to ensure the operation is allowed.
This could result in a SecurityException.
If there is a security manager,
its checkListen
method is first called
with the port
argument
as its argument to ensure the operation is allowed.
This could result in a SecurityException.
If there is a security manager,
its checkListen
method is first called
with the port
argument
as its argument to ensure the operation is allowed.
This could result in a SecurityException.
If the address is null
, then the system will pick up
an ephemeral port and a valid local address to bind the socket.
Any thread currently blocked in {#link receive} upon this socket will throw a SocketException .
If this socket has an associated channel then the channel is closed as well.
If the remote destination to which the socket is connected does not exist, or is otherwise unreachable, and if an ICMP destination unreachable packet has been received for that address, then a subsequent call to send or receive may throw a PortUnreachableException. Note, there is no guarantee that the exception will be thrown.
A caller's permission to send and receive datagrams to a given host and port are checked at connect time. When a socket is connected, receive and send will not perform any security checks on incoming and outgoing packets, other than matching the packet's and the socket's address and port. On a send operation, if the packet's address is set and the packet's address and the socket's address do not match, an IllegalArgumentException will be thrown. A socket connected to a multicast address may only be used to send packets.
The equals
method implements an equivalence relation
on non-null object references:
x
, x.equals(x)
should return
true
.
x
and y
, x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
x
and y
, multiple invocations of
x.equals(y) consistently return true
or consistently return false
, provided no
information used in equals
comparisons on the
objects is modified.
x
,
x.equals(null)
should return false
.
The equals method for class Object
implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x
and
y
, this method returns true
if and only
if x
and y
refer to the same object
(x == y
has the value true
).
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
A datagram socket will have a channel if, and only if, the channel itself was created via the DatagramChannel.open method.
If there is a security manager, its
checkConnect
method is first called
with the host address and -1
as its arguments to see if the operation is allowed.
null
if it is not bound yet.null
if it is unconnected.As the underlying network implementation may ignore the traffic class or type-of-service set using this method may return a different value than was previously set using the method on this DatagramSocket.
java.util.Hashtable
.
The general contract of hashCode
is:
hashCode
method on each of
the two objects must produce the same integer result.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
wait
methods.
The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:
synchronized
statement
that synchronizes on the object.
Class,
by executing a
synchronized static method of that class.
Only one thread at a time can own an object's monitor.
wait
methods.
The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
DatagramPacket
's buffer is filled with
the data received. The datagram packet also contains the sender's
IP address, and the port number on the sender's machine.
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.
If there is a security manager, a packet cannot be received if the
security manager's checkAccept
method
does not allow it.
DatagramPacket
includes information indicating the
data to be sent, its length, the IP address of the remote host,
and the port number on the remote host.
If there is a security manager, and the socket is not currently
connected to a remote address, this method first performs some
security checks. First, if p.getAddress().isMulticastAddress()
is true, this method calls the
security manager's checkMulticast
method
with p.getAddress()
as its argument.
If the evaluation of that expression is false,
this method instead calls the security manager's
checkConnect
method with arguments
p.getAddress().getHostAddress()
and
p.getPort()
. Each call to a security manager method
could result in a SecurityException if the operation is not allowed.
When an application creates a new datagram socket, the socket
implementation factory's createDatagramSocketImpl
method is
called to create the actual datagram socket implementation.
Passing null
to the method is a no-op unless the factory
was already set.
If there is a security manager, this method first calls
the security manager's checkSetFactory
method
to ensure the operation is allowed.
This could result in a SecurityException.
Because SO_RCVBUF is a hint, applications that want to verify what size the buffers were set to should call .
Increasing SO_RCVBUF may allow the network implementation to buffer multiple packets when packets arrive faster than are being received using .
Note: It is implementation specific if a packet larger than SO_RCVBUF can be received.
For UDP sockets it may be necessary to bind more than one socket to the same socket address. This is typically for the purpose of receiving multicast packets (See java.net.MulticastSocket ). The SO_REUSEADDR socket option allows multiple sockets to be bound to the same socket address if the SO_REUSEADDR socket option is enabled prior to binding the socket using .
When a DatagramSocket is created the initial setting of SO_REUSEADDR is disabled.
The behaviour when SO_REUSEADDR is enabled or disabled after a socket is bound (See ) is not defined.
As SO_SNDBUF is a hint, applications that want to verify what size the buffer is should call .
Increasing the buffer size may allow multiple outgoing packets to be queued by the network implementation when the send rate is high.
Note: If
is used to send a
DatagramPacket
that is larger than the setting
of SO_SNDBUF then it is implementation specific if the
packet is sent or discarded.
The tc must be in the range 0 <= tc <=
255
or an IllegalArgumentException will be thrown.
Notes:
for Internet Protocol v4 the value consists of an octet with precedence and TOS fields as detailed in RFC 1349. The TOS field is bitset created by bitwise-or'ing values such the following :-
IPTOS_LOWCOST (0x02)
IPTOS_RELIABILITY (0x04)
IPTOS_THROUGHPUT (0x08)
IPTOS_LOWDELAY (0x10)
Setting bits in the precedence field may result in a SocketException indicating that the operation is not permitted.
for Internet Protocol v6 tc
is the value that
would be placed into the sin6_flowinfo field of the IP header.
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
The current thread must own this object's monitor. The thread
releases ownership of this monitor and waits until another thread
notifies threads waiting on this object's monitor to wake up
either through a call to the notify
method or the
notifyAll
method. The thread then waits until it can
re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.The current thread must own this object's monitor.
This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout); ... // Perform action appropriate to condition }(For more information on this topic, see Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, 2000), or Item 50 in Joshua Bloch's "Effective Java Programming Language Guide" (Addison-Wesley, 2001).
If the current thread is interrupted by another thread while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above.
Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
This method is similar to the wait
method of one
argument, but it allows finer control over the amount of time to
wait for a notification before giving up. The amount of real time,
measured in nanoseconds, is given by:
1000000*timeout+nanos
In all other respects, this method does the same thing as the method of one argument. In particular, wait(0, 0) means the same thing as wait(0).
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until either of the following two conditions has occurred:
notify
method
or the notifyAll
method.
timeout
milliseconds plus nanos
nanoseconds arguments, has
elapsed.
The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout, nanos); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.