Skip to content
Snippets Groups Projects
LandasMulticastReceiver2.java 1.43 KiB
package MV3500Cohort2018JanuaryMarch.homework2;


import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;

/**
 * 
 * @author Rico
 */ 
public class LandasMulticastReceiver2 {
     
    final static String INET_ADDR = "239.1.2.15";
    final static int PORT = 1717;
 
    public static void main(String[] args) throws UnknownHostException {
        // address that we are connecting to
        InetAddress address = InetAddress.getByName(INET_ADDR);
         
        // buffer of bytes, which will be used to store
        // the incoming bytes containing the information from the server
        // ** this stored the excess array slot and printed extra
        //byte[] buf = new byte[256];
         
        // Multicast socket 
        try (MulticastSocket clientSocket = new MulticastSocket(PORT)){
            // Join
            clientSocket.joinGroup(address);
      
            while (true) {
                byte[] buf = new byte[256];
                // Receive information and print
                DatagramPacket msgPacket = new DatagramPacket(buf, buf.length);
                clientSocket.receive(msgPacket);
 
                String msg = new String(buf, 0, buf.length);
                System.out.println("Socket 2 received msg: " + msg);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}