13
|
1 package wifibroadcast;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.net.InetSocketAddress;
|
|
5 import java.net.StandardProtocolFamily;
|
|
6 import java.net.StandardSocketOptions;
|
|
7 import java.nio.channels.SelectionKey;
|
|
8 import java.nio.channels.spi.SelectorProvider;
|
|
9
|
|
10 public class WifiBroadcastChannel extends WifiMulticastChannel {
|
|
11
|
|
12 public WifiBroadcastChannel(int id, int port, SocketType sender) throws IOException {
|
|
13 // join multicast group on this interface, and also use this
|
|
14 // interface for outgoing multicast datagrams
|
|
15 selectMode = false;
|
14
|
16 selector = SelectorProvider.provider().openSelector();
|
|
17 dc = SelectorProvider.provider().openDatagramChannel(StandardProtocolFamily.INET);
|
|
18 dc.setOption(StandardSocketOptions.SO_REUSEADDR, true);
|
13
|
19 mAddr = WifiBroadcast.getBroadcast();
|
|
20 sAddr = new InetSocketAddress(mAddr,port);
|
|
21 dc.setOption(StandardSocketOptions.SO_BROADCAST, true);
|
|
22 if (sender == SocketType.Receiver) {
|
|
23 dc.bind(new InetSocketAddress(port));
|
|
24 if (selectMode) {
|
|
25 dc.configureBlocking(false);
|
|
26 dc.register(selector, SelectionKey.OP_READ);
|
|
27 }
|
|
28 }
|
|
29 }
|
|
30
|
|
31
|
|
32 }
|