-1

I want to broadcast UDP messages in a subnet.

My question is, what address should I put in DatagramSocket and what address in DatagramPacket?

user207421
  • 305,947
  • 44
  • 307
  • 483
Saiful Islam
  • 124
  • 2
  • 14
  • 2
    Have you considered consulting the Javadoc? – user207421 Aug 20 '15 at 15:31
  • Well my confusion is about for broadcasting UDP packets, where should I put the broadcast address? in DatagramSocket() or DatagramPacket()? or in Both? – Saiful Islam Aug 20 '15 at 15:39
  • What exactly do you mean by 'put the broadcast address in DatagramSocket'? Which method are you talking about? And what part of the Javadoc didn't you understand? – user207421 Aug 20 '15 at 15:44
  • ok, first of all, I have to send udp packets to everyone in a subnet. and for that I will have to use DatagramSocket() and DatagramPacket(). Both DatagramSocket() and DatagramPacket() requires a ipAddress and a port for configuring, right? If so, then Which ip address should I put here "DatagramSocket(InetAddress, port)" and which ip address for this one "DatagramPacket(byte, byte.length(), ipAddress, port)". My purpose is to broadcast Udp packets. – Saiful Islam Aug 20 '15 at 16:14
  • Constructing a `DatagramSocket` with an IP:port sets its *local* address. Putting an IP:port into a `DatagramPacket` sets the *target* address. As you are broadcasting, you should put the broadcast address into the packet, and in general you should only supply a port number when constructing the `DatagramSocket`. – user207421 May 02 '17 at 00:10

1 Answers1

1

Take a look at this article. It's about network discovering, but it shows, how you can send broadcasting udp-packages, take a closer look at client example. Shortly, first you send udp-package by default bradcast-address, then you loop all your network interfaces and send udp-packages with each interface's broadcasting address.

What about DatagramSocket and DatagramPacket: to send a packet you can use any DatagramSocket, but you have to set broadcasting address for your DatagramPacket. You need to set an address for the socket in case you need to listen on specific port.

Stanislav
  • 27,441
  • 9
  • 87
  • 82