Skip to content
Snippets Groups Projects
Commit 5bd76085 authored by Michael's avatar Michael
Browse files

Blankenbeker HW 2 Submission; MyMulticastSender, MyMulticastReceiver,

and Truck classes committed.
parent 50684d26
No related branches found
No related tags found
No related merge requests found
File added
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
/**
*
* @author Michael
*/
public class BlankenbekerMulticastReceiver {
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.readFloat();
float secondNumber = dis.readFloat();
float thirdNumber = dis.readFloat();
System.out.println("Number received: " + count + " Vehicle position is: (" + firstNumber + ", " + secondNumber+", "+thirdNumber+")");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
/**
*
* @author Michael
*/
public class BlankenbekerMulticastSender {
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)
{
Truck truck = new Truck();
while (true) {
truck.move();
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);
float x = truck.getX();
float y = truck.getY();
float z = truck.getZ();
dos.writeFloat(x);
dos.writeFloat(y);
dos.writeFloat(z);
byte[] buffer = baos.toByteArray();
// Put together a packet to send
// muticast group we are sending to--not a single host
DatagramPacket packet = 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 < 1; idx++)
{
multicastSocket.send(packet);
Thread.sleep(1000); // Send 100, one per second
System.out.println("Sent new multicast packet 1 of 1");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
\ No newline at end of file
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