I need to send UDP Broadcast
in the network. But I am getting confused. What I know is broadcast is not address specific and Multicast is group (Address) specific.
So, I am using DatagramSocket
for sending and receiving DatagramPackets
from the network.
What code I am writing is:
public class ComputerSender implements Runnable
{
MulticastSocket socket;
DatagramPacket packet;
String command;
public ComputerSender(String MAC)
{
try
{
socket = new MulticastSocket();
JSONManager json = new JSONManager(MAC, WifiConstants.COMPUTER_NET_SCAN);
json.setRecvMAC(WifiConstants.COMPUTER_NETWORK_ADDR);
InetAddress addr = InetAddress.getByName(WifiConstants.COMPUTER_NETWORK_ADDR);
command="Hello";
}
catch(Exception e)
{
Log.v("Exception:","Computer Constructor Error: "+e.toString());
}
}
@Override
public void run()
{
try
{
System.out.println(command);
packet=new DatagramPacket(command.getBytes(),command.getBytes().length,InetAddress.getByName(WifiConstants.COMPUTER_NETWORK_ADDR), WifiConstants.COMPUTER_SEND_PORT);
socket.setTimeToLive(100);
socket.send(packet);
System.out.println("Packet Sent");
Thread.sleep(200);
}
catch(Exception e)
{
Log.v("Packet Sending Error: ","Computer Error: "+e.getMessage());
}
finally
{
socket.close();
}
}
}
I am not able to predict the above code is for Broadcast or Multicast. If Broadcast then what changes I need to make it for Multicast. and if Multicast then what changes I need to bring for Boradcast.