package MV3500Cohort2018JanuaryMarch.homework2; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.MulticastSocket; /** * * @author emilyconard */ public class ConardMulticastSender { /** socket value of shared interest */ public static final String MULTICAST_ADDRESS = "239.1.2.15"; /** socket value of shared interest */ public static final int DESTINATION_PORT = 1717; /** How many routers can be crossed */ public static final int TTL = 10; /** * Program invocation, execution starts here * @param args command-line arguments */ public static void main(String[] args) { try { int ID = 27; int xcord = 5; int ycord = 7; System.setProperty("java.net.preferIPv4Stack", "true"); MulticastSocket multicastSocket = new MulticastSocket(1718); multicastSocket.setTimeToLive(TTL); InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); System.out.println(multicastAddress); // Join group useful on receiving side multicastSocket.joinGroup(multicastAddress); // You can join multiple groups here // Put together a message with binary content. "ByteArrayOutputStream" // is a java.io utility that lets us put together an array of binary // data, which we put into the UDP packet. ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeInt(ID); dos.writeInt(xcord); dos.writeInt(ycord); byte[] buffer = baos.toByteArray(); DatagramPacket packet = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT); for(int idx = 0; idx < 100; idx++) { multicastSocket.send(packet); Thread.sleep(1000); // Send 100, one per second System.out.println("Sent multicast packet " + idx + " of 100"); } } catch(Exception e) { System.out.println(e); } } }