A multicast demonstration. Please run from several machines in the same subnet. Instructions inside file due to crappy GIST description formatting.
import java.io.IOException; | |
import java.net.DatagramPacket; | |
import java.net.InetAddress; | |
import java.net.MulticastSocket; | |
import java.net.NetworkInterface; | |
import java.net.SocketException; | |
import java.net.UnknownHostException; | |
/** | |
* To run: | |
* javac Node.java && java Node; | |
* | |
* Sample output: | |
* node: A8-20-66-55-EF-F3 sending message: A8-20-66-55-EF-F3---0 | |
* node: A8-20-66-55-EF-F3 received: A8-20-66-55-EF-F3---0 | |
*/ | |
public class Node extends Thread { | |
private String id; | |
private MulticastSocket s; | |
private InetAddress group; | |
private int port; | |
//The Multicast Group we wish to join: | |
private static final String MULTICAST_GROUP = "228.5.6.7"; | |
public Node (String id, int port ) throws IOException{ | |
this.id = id; | |
this.group = InetAddress.getByName(MULTICAST_GROUP); | |
this.s = new MulticastSocket(port); | |
this.s.joinGroup(group); | |
this.port = port; | |
} | |
public void send(String msg ) throws IOException{ | |
msg = id + "---"+msg; | |
//Send our MAC plus our unique count. | |
System.out.println("node: "+id + " sending message: "+msg); | |
DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(), group, port); | |
s.send(hi); | |
} | |
/** | |
* @param args | |
* @throws IOException | |
*/ | |
public static void main(String[] args) throws IOException { | |
String id = getMyId(); | |
int portt = 10000; | |
Node n = new Node(id, portt); | |
n.start();//start receiver | |
//sending messages forever | |
int count = 0; | |
while (true){ | |
try { | |
n.send(""+count++); | |
Thread.sleep(10000); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
/** | |
* @return the Mac address as a string | |
* @throws UnknownHostException | |
* @throws SocketException | |
*/ | |
private static String getMyId() throws UnknownHostException, | |
SocketException { | |
InetAddress ip = InetAddress.getLocalHost(); | |
NetworkInterface network = NetworkInterface.getByInetAddress(ip); | |
byte[] ha = network.getHardwareAddress(); | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < ha.length; i++) { | |
sb.append(String.format("%02X%s", ha[i], (i < ha.length - 1) ? "-" : "")); | |
} | |
return sb.toString(); | |
} | |
@Override | |
public void run(){ | |
while (true){ | |
//Just to be sure we get some new buf. too used to C and it giving | |
//you the same uncleared memory arrays. | |
byte[] buf = new byte[1000]; | |
DatagramPacket recv = new DatagramPacket(buf, buf.length); | |
try { | |
s.receive(recv); | |
System.out.println("node: "+id+ " received: "+new String(recv.getData())); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
public void shutdown() throws IOException{ | |
s.leaveGroup(group); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment