Mercurial > hg > Members > kono > WifiBroadcast
view src/wifibroadcast/WifiMulticast.java @ 9:95d58cc5bb0b
instance for test, multiple senders
author | one |
---|---|
date | Sun, 29 Jul 2012 00:03:29 +0900 |
parents | 248ae478dbe6 |
children | e1f43b669cdb |
line wrap: on
line source
package wifibroadcast; import java.io.IOException; import java.nio.ByteBuffer; import java.net.DatagramPacket; import java.net.InetAddress; import java.net.MulticastSocket; public class WifiMulticast implements WifiReceiver { private MulticastSocket soc; private InetAddress mAddr; private int port; public WifiMulticast(int id, String mCASTADDR, int port, SocketType sender) throws IOException { this.port = port; try { mAddr = InetAddress.getByName(mCASTADDR); soc = new MulticastSocket(port); soc.joinGroup(mAddr); } catch (IOException e) { e.printStackTrace(); } } @Override public void recieve(ByteBuffer testData, long timeout) throws IOException { DatagramPacket packet = new DatagramPacket(testData.array(),testData.capacity()); soc.receive(packet); testData.limit(packet.getLength()); testData.position(0); } @Override public void send(ByteBuffer testData) throws IOException { DatagramPacket sendPacket = new DatagramPacket(testData.array(), testData.limit(),mAddr, port); soc.send(sendPacket); testData.position(testData.limit()); } }