Mercurial > hg > Members > kono > WifiBroadcast
annotate src/wifibroadcast/WifiBroadcast.java @ 12:e1f43b669cdb
broadcast on DatagramChannel
author | one |
---|---|
date | Sun, 29 Jul 2012 14:37:12 +0900 |
parents | fc180f38257e |
children | bb2538b7eb8d |
rev | line source |
---|---|
0
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 package wifibroadcast; |
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 |
3 | 3 import java.net.NetworkInterface; |
0
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 import java.io.IOException; |
4 | 5 import java.net.DatagramPacket; |
3 | 6 import java.net.DatagramSocket; |
7 import java.net.InetAddress; | |
0
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 import java.net.InetSocketAddress; |
3 | 9 import java.net.InterfaceAddress; |
0
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 import java.net.SocketException; |
4 | 11 import java.net.UnknownHostException; |
0
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 import java.nio.ByteBuffer; |
3 | 13 import java.util.Enumeration; |
0
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 |
2 | 15 public class WifiBroadcast implements WifiReceiver { |
0
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 |
3 | 17 private DatagramSocket s; |
4 | 18 private int port; |
19 private InetAddress mAddr; | |
0
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 |
9 | 21 public WifiBroadcast(int id,int port, SocketType sender) throws IOException { |
4 | 22 this.port = port; |
23 | |
3 | 24 if (sender == SocketType.Sender) { |
25 try { | |
26 InetAddress address0 = getBroadcast(); | |
27 System.out.println("Found broadcast "+address0); | |
4 | 28 mAddr = address0; |
29 s = new DatagramSocket(); | |
9 | 30 s.bind(new InetSocketAddress(address0,port+1+id)); |
4 | 31 s.setBroadcast(true); |
3 | 32 } catch (SocketException e) { |
33 } | |
34 } else { | |
4 | 35 // InetAddress address0 = getBroadcast(); |
36 s = new DatagramSocket(port); | |
37 // s.bind(new InetSocketAddress(address0,port)); | |
38 s.setReuseAddress(true); | |
39 s.setBroadcast(true); | |
0
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 } |
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41 } |
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 |
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43 public void recieve(ByteBuffer testData, long timeout) throws IOException { |
4 | 44 DatagramPacket packet = new DatagramPacket(testData.array(),testData.capacity()); |
45 s.receive(packet); | |
46 testData.limit(packet.getLength()); | |
47 testData.position(0); | |
48 System.out.println("recv "+packet.getLength()+" bytes from "+packet.getAddress()); | |
49 return; // one at a time | |
50 } | |
51 | |
52 | |
53 public void send(ByteBuffer testData) throws IOException { | |
54 DatagramPacket sendPacket = new DatagramPacket(testData.array(), testData.limit(),mAddr, port); | |
55 s.send(sendPacket); | |
56 testData.position(testData.limit()); | |
57 System.out.println("send"); | |
0
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58 } |
3 | 59 |
4 | 60 public InetAddress getBroadcast0() throws UnknownHostException { |
61 return InetAddress.getByName("192.168.100.66"); | |
62 } | |
63 | |
12 | 64 public static InetAddress getBroadcast() throws SocketException { |
3 | 65 Enumeration<NetworkInterface> interfaces = |
66 NetworkInterface.getNetworkInterfaces(); | |
67 while (interfaces.hasMoreElements()) { | |
68 NetworkInterface networkInterface = interfaces.nextElement(); | |
69 if (networkInterface.isLoopback()) | |
70 continue; // Don't want to broadcast to the loopback interface | |
71 for (InterfaceAddress interfaceAddress : | |
72 networkInterface.getInterfaceAddresses()) { | |
73 InetAddress broadcast = interfaceAddress.getBroadcast(); | |
74 if (broadcast == null) | |
75 continue; | |
76 // Use the address | |
10 | 77 System.out.println("MTU="+networkInterface.getMTU()); |
3 | 78 return broadcast; |
79 } | |
80 } | |
81 throw new SocketException(); | |
82 } | |
0
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
83 |
1 | 84 |
0
df9d16620c08
Wifi broad cast test program
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
85 } |