开发者

Multicast ping echoing back to my client

开发者 https://www.devze.com 2023-03-12 04:04 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消