Something went wrong on our end
-
Brutzman, Don authoredBrutzman, Don authored
LandasMulticastReceiver.java 1.93 KiB
package MV3500Cohort2018JanuaryMarch.homework2;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
/**
* homework assignment
* @author Rico
*/
public class LandasMulticastReceiver
{
/**
* Default constructor to silence javadoc warning
* @see <a href="https://stackoverflow.com/questions/4488716/java-default-constructor" target="_blank">StackOverflow: Java default constructor</a>
*/
public LandasMulticastReceiver ()
{
// default initializations occur here
}
final static String INET_ADDR = "239.1.2.15";
final static int PORT = 1717;
/** run the program
* @param args command-line arguments, string parameters (unused)
* @throws java.net.UnknownHostException user cancels program */
@SuppressWarnings("deprecation")
public static void main(String[] args) throws UnknownHostException
{
// address that we are connecting to
InetAddress address = InetAddress.getByName(INET_ADDR);
// buffer of bytes
// 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 1 received msg: " + msg);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}