0

I send messages to WIFI Access Point via MulticastSocket and get replies always twice. If I try to send message to me self, I get message twice again. This is my receiver code:

protected Void doInBackground(Void... params) {

                String lText;
                byte[] lMsg = new byte[GlobalConfig.MAX_UDP_DATAGRAM_LEN];
                DatagramPacket dp = new DatagramPacket(lMsg, lMsg.length);
                MulticastSocket ds = null;
                try {
                    ds = new MulticastSocket (32001);
                    InetAddress serverAddr = InetAddress.getByName("224.237.124.120");
                    ds.joinGroup(serverAddr);
                    while (serverActive) {

                        ds.receive(dp);
                        Log.d("UDP packet received", dp.toString());
                        lText = new String(lMsg, 0, dp.getLength());
                        receivedMessage = lText;
                        doSomething();

                    }
                } catch (SocketException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (ds != null) {
                        ds.close();
                    }
                }
                return null;
            }

I tried to send via DatagramSocket and via MulticastSocket - no matter. I get messages alway twice. I don't understand why!

EDIT: my LogCat:

I/GatewayController﹕ Message Sent
...

D/UDP packet received﹕ java.net.DatagramPacket@422dc860
D/UDP packet received﹕ java.net.DatagramPacket@422dc860

EDIT2: Sender code

protected Void doInBackground(Void... params) {

                DatagramSocket ds = null;
                try {
                    ds = new DatagramSocket();
                    InetAddress serverAddr = InetAddress.getByName("224.237.124.120");
                    DatagramPacket dp;
                    dp = new DatagramPacket(byteMsg, byteMsg.length,
                            serverAddr, 32000);
                    ds.send(dp);
Alexander Tumanin
  • 1,638
  • 2
  • 23
  • 37

2 Answers2

0

I tried to send via DatagramSocket and via MulticastSocket - no matter. I get messages alway twice. I don't understand why!

EDIT: my LogCat:

I/GatewayController﹕ Message Sent ...
D/UDP packet received﹕ java.net.DatagramPacket@422dc860 D/UDP packet received﹕ java.net.DatagramPacket@422dc860

This is not evidence that you received the same message twice. It is evidence that you always receive into the same byte array. Try logging the message content.

However multicast is UDP, and UDP does not guarantee delivery, or single delivery, or sequenced delivery, so it remains possible you will get duplicates. If that's semantically important, you need to detect it via sequence numbers.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

Here is the right way:

InetAddress group = InetAddress.getByName(GlobalConfig.MULTICAST_IP);
SocketAddress sockaddr = new InetSocketAddress(group,GlobalConfig.LOCAL_PORT);
ds = new MulticastSocket(sockaddr);
ds.joinGroup(group);

This is important, but difficult to find in examples in Internet:

SocketAddress sockaddr = new InetSocketAddress(group,GlobalConfig.LOCAL_PORT);
ds = new MulticastSocket(sockaddr);
user207421
  • 305,947
  • 44
  • 307
  • 483
Alexander Tumanin
  • 1,638
  • 2
  • 23
  • 37