Skip to content
Snippets Groups Projects
Commit 04568344 authored by emilyconard's avatar emilyconard
Browse files

No commit message

No commit message
parent 15440b78
No related branches found
No related tags found
No related merge requests found
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
/**
*
* @author emilyconard
*/
public class ConardMulticastReceiver {
public static final String MULTICAST_ADDRESS = "239.1.2.15";
public static final int DESTINATION_PORT = 1717;
/** How many routers can be crossed */
public static final int TTL = 10;
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(DESTINATION_PORT);
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
int count = 0;
while(true)
{
byte[] packetArray = new byte[1500];
DatagramPacket packet = new DatagramPacket(packetArray, packetArray.length);
multicastSocket.receive(packet);
count++;
ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData());
DataInputStream dis = new DataInputStream(bais);
float firstNumber = dis.readInt();
float secondNumber = dis.readInt();
float thirdNumber = dis.readInt();
System.out.println("Number received: " + count + " ID: " + firstNumber + " xcord: " + secondNumber + " ycord: "+ thirdNumber);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment