-1

I want to send some messages to other machines by using UDP protocol in Java. And I knew my ip address and the broadcast address it should advertise on. Like my ip address is 127.0.0.1, and the I the other address is 127.0.0.200. I want to broadcast the message to machines with addresses from 127.0.0.1 to 127.0.0.200. How to implement this in Java?

And the range of ip address may be 127.0.0.1 to 127.1.4.80.

I am a beginner and I just know I should use InetAddress type to express the ip address. But I don't know how to iterate all the InetAddress objects.

Could someone help me?

Thanks a lot!!

1 Answers1

0

You need:

  1. A DatagramSocket.
  2. Some data in a byte[] array.
  3. A target address:port, expressed as an InetSocketAddress.
  4. A DatagramPacket, created from the byte[] array and the target address.
  5. DatagramSocket.send().

You can't use 127. anything if you want to talk to other hosts. Those addresses are localhost-only.

user207421
  • 305,947
  • 44
  • 307
  • 483