Skip to content
Snippets Groups Projects
Commit 6d4369b4 authored by Don McGregor's avatar Don McGregor
Browse files

multicast example

parent 4acad380
No related branches found
No related tags found
No related merge requests found
build.xml.data.CRC32=d1f74d73
build.xml.script.CRC32=894241e6
build.xml.stylesheet.CRC32=8064a381@1.80.1.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=d1f74d73
nbproject/build-impl.xml.script.CRC32=46485cbc
nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48
application.args=-Djava.net.preferIPv4Stack=true
compile.on.save=true
do.depend=false
do.jar=true
javac.debug=true
javadoc.preview=true
user.properties.file=/Users/mcgredo/Library/Application Support/NetBeans/8.2/build.properties
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/Users/mcgredo/projects/gitlab/NetworkedGraphicsMV3500/projects/multicastExample1/MulticastSenderExample/src/multicastsenderexample/MulticastSenderExample.java</file>
</group>
</open-files>
</project-private>
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
application.title=MulticastSenderExample
application.vendor=mcgredo
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
${run.classpath}
debug.test.classpath=\
${run.test.classpath}
# Files in build.classes.dir which should be excluded from distribution jar
dist.archive.excludes=
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/MulticastSenderExample.jar
dist.javadoc.dir=${dist.dir}/javadoc
endorsed.classpath=
excludes=
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.external.vm=true
javac.processorpath=\
${javac.classpath}
javac.source=1.8
javac.target=1.8
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}
javac.test.processorpath=\
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
main.class=multicastsenderexample.MulticastSenderExample
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
source.encoding=UTF-8
src.dir=src
test.src.dir=test
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>MulticastSenderExample</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
</configuration>
</project>
package multicastsenderexample;
import java.io.*;
import java.net.*;
/**
* Looks a lot like a UDP sender.
*
* @author mcgredo
*/
public class MulticastSenderExample {
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(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);
dos.writeFloat(17.0f);
dos.writeFloat(23.0f);
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 < 100; idx++)
{
multicastSocket.send(packet);
Thread.sleep(1000); // Send 100, one per second
System.out.println("Sent multicast packet " + idx + " of 100");
}
}
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