Construct a DatagramSocket
object on the port on which you want to listen.
Pass an empty DatagramPacket
object to the DatagramSocket
's receive()
method.
public synchronized void receive(DatagramPacket dp) throws IOException
The calling thread blocks until a datagram is received.
dp
is filled
with the data from that datagram.
Use getPort()
and and getAddress()
to tell where the
packet came from, getData()
to
retrieve the data, and getLength()
to
see how many bytes were in the data.
If the received packet was too long for the buffer, it's truncated to the length of the buffer.
Length is reset when packet is received