view src/main/jp/ac/uryukyu/ie/kono/wifibroadcast/WifiBroadcastChannel.java @ 26:42ecbd9364fa

gradle
author oshiroo
date Wed, 28 Nov 2018 17:34:23 +0900
parents src/wifibroadcast/WifiBroadcastChannel.java@313dc81af36c
children
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
		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);
			}
		}
	}


}