view src/wifibroadcast/WifiBroadcastChannel.java @ 13:9bf4bf393e4e

hoge
author one
date Sun, 29 Jul 2012 15:55:03 +0900
parents
children 7b43c3c60708
line wrap: on
line source

package wifibroadcast;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.spi.SelectorProvider;

public class WifiBroadcastChannel extends WifiMulticastChannel {

	public WifiBroadcastChannel(int id, int port, SocketType sender) throws IOException {
		// join multicast group on this interface, and also use this
		// interface for outgoing multicast datagrams
		selectMode = false;
		selector = SelectorProvider.provider().openSelector();	

		dc = DatagramChannel.open(StandardProtocolFamily.INET)
				.setOption(StandardSocketOptions.SO_REUSEADDR, true);
		mAddr = WifiBroadcast.getBroadcast();
		sAddr = new InetSocketAddress(mAddr,port);
		dc.setOption(StandardSocketOptions.SO_BROADCAST, true);
		if (sender == SocketType.Receiver) {
			dc.bind(new InetSocketAddress(port));
			if (selectMode) {
				dc.configureBlocking(false);
				dc.register(selector, SelectionKey.OP_READ);
			}
		}
	}


}