Mercurial > hg > Members > kono > WifiBroadcast
changeset 2:2a328333ba70
no compile errors
author | one |
---|---|
date | Sat, 28 Jul 2012 13:06:57 +0900 |
parents | 649b8573372c |
children | 9c99e2193277 |
files | src/wifibroadcast/WifiBroadcast.java src/wifibroadcast/WifiBroadcastTest.java src/wifibroadcast/WifiMulticast.java src/wifibroadcast/WifiMulticastChannel.java |
diffstat | 4 files changed, 26 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/src/wifibroadcast/WifiBroadcast.java Sat Jul 28 12:24:04 2012 +0900 +++ b/src/wifibroadcast/WifiBroadcast.java Sat Jul 28 13:06:57 2012 +0900 @@ -11,7 +11,7 @@ import java.nio.channels.spi.SelectorProvider; import java.util.Iterator; -public class WifiBroadcast implements WifiReceiver,WifiSender { +public class WifiBroadcast implements WifiReceiver { private DatagramChannel channel; private Selector selector; @@ -20,12 +20,13 @@ selector = SelectorProvider.provider().openSelector(); channel = SelectorProvider.provider().openDatagramChannel(); channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); + channel.configureBlocking(false); try { InetSocketAddress address = new InetSocketAddress("::", port); - channel.socket().bind(address); + channel.connect(address); } catch (SocketException e) { // for some bad IPv6 implementation - channel.socket().bind(new InetSocketAddress(port)); + channel.connect(new InetSocketAddress(port)); } channel.register(selector, SelectionKey.OP_READ); }
--- a/src/wifibroadcast/WifiBroadcastTest.java Sat Jul 28 12:24:04 2012 +0900 +++ b/src/wifibroadcast/WifiBroadcastTest.java Sat Jul 28 13:06:57 2012 +0900 @@ -3,11 +3,13 @@ import java.io.IOException; import java.nio.ByteBuffer; +import javax.sql.rowset.Joinable; + public class WifiBroadcastTest { private static int DefaultPort = 8212; private static String MCASTADDR = "224.0.0.1"; - static void main(String args[]) { + public static void main(String args[]) { int port = DefaultPort ; int count = 1024; long timeout = 1000; @@ -53,12 +55,18 @@ for(int i = 0; i<count;i++) { testData.putInt(0, i); wbs.send(testData); + testData.flip(); } } catch (IOException e) { } } }; - new Thread(sender).start(); + Thread s = new Thread(sender); + s.start(); + try { + s.join(); + } catch (InterruptedException e) { + } } private static boolean running; @@ -82,7 +90,9 @@ int num = 0, bad = 0; try { for(int i = 0; running && i<count;i++) { + testData.clear(); wbr.recieve(testData,timeout); + if (!testData.hasRemaining()) continue; int seq = testData.getInt(); if (seq!=i) { bad++; i = seq; @@ -93,8 +103,13 @@ System.out.println("get "+num+" packets, "+bad+" losts."); } }; - new Thread(receiver).start(); - new Thread(timeouter).start(); + Thread r = new Thread(receiver); r.start(); + Thread t = new Thread(timeouter); t.start(); + try { + r.join(); + t.join(); + } catch (InterruptedException e) { + } } public static ByteBuffer getTestData(int i) {
--- a/src/wifibroadcast/WifiMulticast.java Sat Jul 28 12:24:04 2012 +0900 +++ b/src/wifibroadcast/WifiMulticast.java Sat Jul 28 13:06:57 2012 +0900 @@ -8,7 +8,7 @@ import java.net.InetAddress; import java.net.MulticastSocket; - public class WifiMulticast implements WifiReceiver,WifiSender { + public class WifiMulticast implements WifiReceiver { private MulticastSocket soc; private InetAddress mAddr; private int port;
--- a/src/wifibroadcast/WifiMulticastChannel.java Sat Jul 28 12:24:04 2012 +0900 +++ b/src/wifibroadcast/WifiMulticastChannel.java Sat Jul 28 13:06:57 2012 +0900 @@ -10,7 +10,7 @@ import java.nio.ByteBuffer; import java.nio.channels.DatagramChannel; -public class WifiMulticastChannel implements WifiReceiver,WifiSender { +public class WifiMulticastChannel implements WifiReceiver { private InetAddress mAddr; private DatagramChannel dc; private SocketAddress sAddr; @@ -18,7 +18,7 @@ public WifiMulticastChannel(String mCASTADDR, int port) throws IOException { // join multicast group on this interface, and also use this // interface for outgoing multicast datagrams - NetworkInterface ni = NetworkInterface.getByName("en0"); + NetworkInterface ni = NetworkInterface.getByName("en1"); dc = DatagramChannel.open(StandardProtocolFamily.INET) .setOption(StandardSocketOptions.SO_REUSEADDR, true)