So I am sending a multicast packet to discover any services available and the first reply I get is the message I sent in the first place.
// create socket
InetAddress addr = InetAddress.getByName(host);
MulticastSocket socket = new MulticastSocket(port);
socket.joinGroup(addr);
// send ping
byte[] byteArray = PING.getBytes();
DatagramPacket packet =
new DatagramPacket(byteArray, byteArray.length, addr, port);
socket.send(packet, (byte) 255);
// listen for pong
byteArray = PONG.getBytes();
packet = new DatagramPacket(byteArray, byteArray.length);
socket.setSoTimeout(2000);
socket.receive(packet); // TODO: first message is echo from me!!!
socket.receive(packet);
String response = new String(packet.getData());
Toast toast = Toast.makeText(context, "Received '" + response +
"' from " + packet.getAddress(), Toast.LENGTH_LONG);
toast.show();
How do I开发者_如何学Go stop that from happening?
You want to set the multicast socket loopback mode:
socket.setLoopbackMode(true); // Amusingly, this parameter is a disable
More info can be found at the Android site.
精彩评论