Skip to content
Snippets Groups Projects
MaroonMulticastSenderExample.java 3.55 KiB
package MV3500Cohort2018JanuaryMarch.homework2;




import java.io.*;
import java.net.*;

/**
 * Looks a lot like a UDP sender.
 * 
 * @author mcgredo
 */
public class MaroonMulticastSenderExample
{
    /** socket parameter of interest */
    public static final String MULTICAST_ADDRESS = "239.1.2.15";
    /** socket parameter of interest */
    public static final int    DESTINATION_PORT = 1717;
    /** How many routers can be crossed */
    static final int TTL = 10; // Time to Live is decrement counter to limit scope
    
    /** run the program
     * @param args command-line arguments, string parameters (unused) */
    @SuppressWarnings("deprecation")
    public static void main(String[] args) 
    {
        try
        {
            // This is a java/IPv6 problem. You should also add it to the
            // arguments used to start the app, eg -Djava.net.preferIPv4Stack=true
            // set in the "run" section of preferences. Also, typically
            // netbeans must be restarted after these settings.
            // https://stackoverflow.com/questions/18747134/getting-cant-assign-requested-address-java-net-socketexception-using-ehcache
            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(1);
            String message = "**********************************";
            dos.writeInt(message.length());
            dos.writeChars(message);
            byte[] buffer = baos.toByteArray();
            
            DatagramPacket packet1 = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT);
            
            baos.reset();
            dos.writeInt(2);
            message = "<===========================>";
            
            dos.writeInt(message.length());
            dos.writeChars(message);
            buffer = baos.toByteArray();
            
            DatagramPacket packet2 = new DatagramPacket(buffer, buffer.length, multicastAddress, DESTINATION_PORT);
            
            
            // How fast does this go? Does UDP try to slow it down, or does
            // this cause network problems? (hint: yes for an unlimited send
            // rate, unlike TCP). How do you know on the receiving side
            // that you haven't received a duplicate UDP packet, out of
            // order packet, or dropped packet?
            
            for(int idx = 0; idx < 1000; idx++)
            {
               
               multicastSocket.send(packet1);
               multicastSocket.send(packet2);
               
               Thread.sleep(1000); // Send 100, one per second
               System.out.println("Sent multicast packet " + idx + " of 100");
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
    
}