view src/wifibroadcast/WifiMulticastChannel.java @ 2:2a328333ba70

no compile errors
author one
date Sat, 28 Jul 2012 13:06:57 +0900
parents 649b8573372c
children 9c99e2193277
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 WifiReceiver {
	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("en1");

	     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);
	}

}