view src/wifibroadcast/WifiBroadcastChannel.java @ 14:7b43c3c60708

tcp
author one
date Mon, 30 Jul 2012 14:04:22 +0900
parents 9bf4bf393e4e
children 313dc81af36c
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.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 = SelectorProvider.provider().openDatagramChannel(StandardProtocolFamily.INET);
		dc.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);
			}
		}
	}


}