Mercurial > hg > Members > kono > WifiBroadcast
changeset 4:404778ac3f55
worked. Broadcast, Multicast, Multicast channel.
author | one |
---|---|
date | Sat, 28 Jul 2012 19:33:34 +0900 |
parents | 9c99e2193277 |
children | 7679d10bdae9 |
files | src/wifibroadcast/WifiBroadcast.java src/wifibroadcast/WifiBroadcastTest.java src/wifibroadcast/WifiDatagram.java |
diffstat | 3 files changed, 101 insertions(+), 48 deletions(-) [+] |
line wrap: on
line diff
--- a/src/wifibroadcast/WifiBroadcast.java Sat Jul 28 18:15:13 2012 +0900 +++ b/src/wifibroadcast/WifiBroadcast.java Sat Jul 28 19:33:34 2012 +0900 @@ -2,71 +2,65 @@ import java.net.NetworkInterface; import java.io.IOException; +import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.InterfaceAddress; import java.net.SocketException; -import java.net.StandardSocketOptions; +import java.net.UnknownHostException; import java.nio.ByteBuffer; -import java.nio.channels.DatagramChannel; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.spi.SelectorProvider; import java.util.Enumeration; -import java.util.Iterator; public class WifiBroadcast implements WifiReceiver { - private DatagramChannel channel; - private Selector selector; private DatagramSocket s; + private int port; + private InetAddress mAddr; public WifiBroadcast(int port, SocketType sender) throws IOException { - selector = SelectorProvider.provider().openSelector(); - channel = SelectorProvider.provider().openDatagramChannel(); - s = channel.socket(); - channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); - channel.configureBlocking(false); + this.port = port; + if (sender == SocketType.Sender) { - s.setBroadcast(true); try { InetAddress address0 = getBroadcast(); System.out.println("Found broadcast "+address0); - InetSocketAddress address = new InetSocketAddress(address0, port); - channel.connect(address); + mAddr = address0; + s = new DatagramSocket(); + s.bind(new InetSocketAddress(address0,port+1)); + s.setBroadcast(true); } catch (SocketException e) { - // for some bad IPv6 implementation - channel.connect(new InetSocketAddress(port)); } } else { - try { - InetSocketAddress address = new InetSocketAddress("::", port); - channel.connect(address); - } catch (SocketException e) { - // for some bad IPv6 implementation - channel.connect(new InetSocketAddress(port)); - } + // InetAddress address0 = getBroadcast(); + s = new DatagramSocket(port); + // s.bind(new InetSocketAddress(address0,port)); + s.setReuseAddress(true); + s.setBroadcast(true); } - channel.register(selector, SelectionKey.OP_READ); } public void recieve(ByteBuffer testData, long timeout) throws IOException { - if (selector.select(timeout)>0) { - for (Iterator<SelectionKey> it = selector.selectedKeys().iterator();it.hasNext(); ) { - SelectionKey s = it.next(); - it.remove(); - DatagramChannel ch = (DatagramChannel)s.channel(); - ch.read(testData); - testData.flip(); - return; // one at a time - } - } else { - System.out.println("receiver time out"); - testData.position(0); testData.limit(0); - } + DatagramPacket packet = new DatagramPacket(testData.array(),testData.capacity()); + s.receive(packet); + testData.limit(packet.getLength()); + testData.position(0); + System.out.println("recv "+packet.getLength()+" bytes from "+packet.getAddress()); + return; // one at a time + } + + + public void send(ByteBuffer testData) throws IOException { + DatagramPacket sendPacket = new DatagramPacket(testData.array(), testData.limit(),mAddr, port); + s.send(sendPacket); + testData.position(testData.limit()); + System.out.println("send"); } + public InetAddress getBroadcast0() throws UnknownHostException { + return InetAddress.getByName("192.168.100.66"); + } + public InetAddress getBroadcast() throws SocketException { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); @@ -87,8 +81,4 @@ } - public void send(ByteBuffer testData) throws IOException { - int out = channel.write(testData); - System.out.println("send "+out+" bytes."); - } }
--- a/src/wifibroadcast/WifiBroadcastTest.java Sat Jul 28 18:15:13 2012 +0900 +++ b/src/wifibroadcast/WifiBroadcastTest.java Sat Jul 28 19:33:34 2012 +0900 @@ -7,11 +7,11 @@ private static int DefaultPort = 8412; private static String MCASTADDR = "224.0.0.1"; private static int testSize = 256; + private static long timeout = 1000; public static void main(String args[]) { int port = DefaultPort ; int count = 16; - long timeout = 1000; boolean multicast = false; boolean mchannel = false; WifiReceiver wbr = null; @@ -26,15 +26,15 @@ try { if (multicast) { wbr = new WifiMulticast(MCASTADDR,port,WifiReceiver.SocketType.Receiver); - Thread.sleep(5000); + Thread.sleep(timeout); wbs = new WifiMulticast(MCASTADDR,port,WifiReceiver.SocketType.Receiver); } else if (mchannel) { wbr = new WifiMulticastChannel(MCASTADDR,port,WifiReceiver.SocketType.Receiver); - Thread.sleep(5000); + Thread.sleep(timeout); wbs = new WifiMulticastChannel(MCASTADDR,port,WifiReceiver.SocketType.Sender); } else { wbr = new WifiBroadcast(port,WifiReceiver.SocketType.Receiver); - Thread.sleep(5000); + Thread.sleep(timeout); wbs = new WifiBroadcast(port,WifiReceiver.SocketType.Sender); } } catch (IOException e) { @@ -63,7 +63,7 @@ ByteBuffer testData = getTestData(testSize); int i = 0; try { - Thread.sleep(1000); + Thread.sleep(timeout); for(i = 0; i<count;i++) { testData.putInt(0, i); wbs.send(testData);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/wifibroadcast/WifiDatagram.java Sat Jul 28 19:33:34 2012 +0900 @@ -0,0 +1,63 @@ +package wifibroadcast; + +import java.net.NetworkInterface; +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.InterfaceAddress; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.nio.ByteBuffer; +import java.util.Enumeration; + +public class WifiDatagram implements WifiReceiver { + + private DatagramSocket s; + private int port; + private InetAddress mAddr; + + public WifiDatagram(int port, SocketType sender) throws IOException { + this.port = port; + + if (sender == SocketType.Sender) { + try { + InetAddress address0 = getLocalAddress(); + System.out.println("Found broadcast "+address0); + mAddr = address0; + s = new DatagramSocket(); + s.bind(new InetSocketAddress(address0,port+1)); + s.setBroadcast(true); + } catch (SocketException e) { + } + } else { + // InetAddress address0 = getBroadcast(); + s = new DatagramSocket(port); + // s.bind(new InetSocketAddress(address0,port)); + s.setReuseAddress(true); + s.setBroadcast(true); + } + } + + public void recieve(ByteBuffer testData, long timeout) throws IOException { + DatagramPacket packet = new DatagramPacket(testData.array(),testData.capacity()); + s.receive(packet); + testData.limit(packet.getLength()); + testData.position(0); + return; // one at a time + } + + + public void send(ByteBuffer testData) throws IOException { + DatagramPacket sendPacket = new DatagramPacket(testData.array(), testData.limit(),mAddr, port); + s.send(sendPacket); + testData.position(testData.limit()); + System.out.println("send"); + } + + public InetAddress getLocalAddress() throws UnknownHostException { + return InetAddress.getByName("127.0.0.1"); + } + +}