Mercurial > hg > Members > kono > WifiBroadcast
view src/wifibroadcast/WifiMulticastChannel.java @ 0:df9d16620c08
Wifi broad cast test program
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 28 Jul 2012 12:16:49 +0900 |
parents | |
children | 649b8573372c |
line wrap: on
line source
package wifibroadcast; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.NetworkInterface; import java.net.SocketAddress; import java.net.StandardProtocolFamily; import java.net.StandardSocketOptions; import java.nio.ByteBuffer; import java.nio.channels.DatagramChannel; public class WifiMulticastChannel implements WifiReciver,WifiSender { private InetAddress mAddr; private DatagramChannel dc; private SocketAddress sAddr; public WifiMulticastChannel(String mCASTADDR, int port) throws IOException { // join multicast group on this interface, and also use this // interface for outgoing multicast datagrams NetworkInterface ni = NetworkInterface.getByName("en0"); dc = DatagramChannel.open(StandardProtocolFamily.INET) .setOption(StandardSocketOptions.SO_REUSEADDR, true) .bind(sAddr = new InetSocketAddress(port)) .setOption(StandardSocketOptions.IP_MULTICAST_IF, ni); mAddr = InetAddress.getByName(mCASTADDR); dc.join(mAddr, ni); } @Override public void recieve(ByteBuffer testData, long timeout) throws IOException { dc.receive(testData); } @Override public void send(ByteBuffer testData) throws IOException { dc.send(testData, sAddr); } }