Skip to content
Snippets Groups Projects
Commit 7234ee9b authored by Brian's avatar Brian
Browse files

Merge origin/master

parents b642cadf 4d9f599b
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,11 @@ ...@@ -3,6 +3,11 @@
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/** /**
* *
...@@ -14,57 +19,78 @@ public class MaroonMulticastReceiver { ...@@ -14,57 +19,78 @@ public class MaroonMulticastReceiver {
public static final int DESTINATION_PORT = 1717; public static final int DESTINATION_PORT = 1717;
/** How many routers can be crossed */ /** How many routers can be crossed */
public static final int TTL = 10; public static final int TTL = 10;
public static void main(String[] args) public static void main(String[] args)
{ {
try JFrame guiFrame = new JFrame();
{ guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Read a message");
guiFrame.setSize(300,250);
guiFrame.setLocationRelativeTo(null);
final JPanel output = new JPanel();
output.setVisible(true);
JLabel outputLbl= new JLabel("Hit the Button");
output.add(outputLbl);
JButton MCButton = new JButton( "Push Me");
MCButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent event){
try {
// This is a java/IPv6 problem. You should also add it to the // This is a java/IPv6 problem. You should also add it to the
// arguments used to start the app, eg -Djava.net.preferIPv4Stack=true // arguments used to start the app, eg -Djava.net.preferIPv4Stack=true
// set in the "run" section of preferences. Also, typically // set in the "run" section of preferences. Also, typically
// netbeans must be restarted after these settings. // netbeans must be restarted after these settings.
// https://stackoverflow.com/questions/18747134/getting-cant-assign-requested-address-java-net-socketexception-using-ehcache // https://stackoverflow.com/questions/18747134/getting-cant-assign-requested-address-java-net-socketexception-using-ehcache
System.setProperty("java.net.preferIPv4Stack", "true"); System.setProperty("java.net.preferIPv4Stack", "true");
MulticastSocket multicastSocket = new MulticastSocket(DESTINATION_PORT); MulticastSocket multicastSocket = new MulticastSocket(DESTINATION_PORT);
multicastSocket.setTimeToLive(TTL); multicastSocket.setTimeToLive(TTL);
InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS); InetAddress multicastAddress = InetAddress.getByName(MULTICAST_ADDRESS);
System.out.println(multicastAddress); System.out.println(multicastAddress);
// Join group useful on receiving side // Join group useful on receiving side
multicastSocket.joinGroup(multicastAddress); multicastSocket.joinGroup(multicastAddress);
// You can join multiple groups here // You can join multiple groups here
int count = 0; int count = 0;
while(true) byte[] packetArray = new byte[1500];
{ DatagramPacket packet = new DatagramPacket(packetArray, packetArray.length);
byte[] packetArray = new byte[1500];
DatagramPacket packet = new DatagramPacket(packetArray, packetArray.length);
multicastSocket.receive(packet); multicastSocket.receive(packet);
count++; count++;
ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData()); ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData());
DataInputStream dis = new DataInputStream(bais); DataInputStream dis = new DataInputStream(bais);
int firstNumber = dis.readInt(); int firstNumber = dis.readInt();
int messageLength = dis.readInt(); int messageLength = dis.readInt();
String message = ""; String message = "";
for (int i = 0; i < messageLength; i++){ for (int i = 0; i < messageLength; i++){
message += dis.readChar(); message += dis.readChar();
} }
outputLbl.setText(message);
System.out.println("Number received: " + count + " First number:" + firstNumber + " message:" + message);
System.out.println("Number received: " + count + " First number:" + firstNumber + " message:" + message); }
catch(Exception e){
System.out.println(e);
}
} }
} }
catch(Exception e) );
{
System.out.println(e);
} guiFrame.add(output, BorderLayout.CENTER);
guiFrame.add(MCButton,BorderLayout.NORTH);
guiFrame.setVisible(true);
} }
} }
...@@ -43,7 +43,7 @@ public class MaroonMulticastSenderExample { ...@@ -43,7 +43,7 @@ public class MaroonMulticastSenderExample {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos); DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(1); dos.writeInt(1);
String message = "Hammertime"; String message = "**********************************";
dos.writeInt(message.length()); dos.writeInt(message.length());
dos.writeChars(message); dos.writeChars(message);
byte[] buffer = baos.toByteArray(); byte[] buffer = baos.toByteArray();
...@@ -52,7 +52,7 @@ public class MaroonMulticastSenderExample { ...@@ -52,7 +52,7 @@ public class MaroonMulticastSenderExample {
baos.reset(); baos.reset();
dos.writeInt(2); dos.writeInt(2);
message = "Collaberate and Listen"; message = "<===========================>";
dos.writeInt(message.length()); dos.writeInt(message.length());
dos.writeChars(message); dos.writeChars(message);
......
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