How to send to receive datagram packet?

How to send to receive datagram packet?

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.

One can create a packet as:
DatagramPacket packet = new DatagramPacket(message, message.length, address, DAYTIME_PORT);

To send or receive a packet, the sockets send or receive methods can be used:
socket.send(packet);
socket.receive(packet);
Difference between field variable and local variable
Field variables: Variable that are declared as a member of a class. OR Variables declared outside any method/constructor but inside the class block......
When do we need to flush an output stream?
If any bytes that are previously written and have been buffered by the implementation of the output stream...
Explain the purpose of garbage collection that the JVM uses
Garbage collection in Java identifies and discards the objects that are no longer needed by a program....
Post your comment