Java - android UDP unreliability

I have extreme UDP packet loss with Android, and that doesn't make sense. The situation is as follows:

  • The client works with a java client connected to the home network.
  • The phone works with a java server (Android) connected to the home network.
  • The home router is the new WRT1900ac. The network has an internet connection.
  • UDP packets are small (<15 bytes)

Symptoms

  • If a PC sends a UDP packet to another computer (both on the same network), it works very well (almost no packets are lost).

  • If Android sends a UDP packet to a PC on the same network, it also works very well (almost no packets are lost).

  • If the PC sends UDP to Android on the same network, I get extreme packet loss (50% of the time or more - but it changes).

, 10 , . . , . android pc, java UDP- Packet Sender, . , , , , , , . , , Android- ​​ . Android , , . Android ( ). - ...

PC- Packet Sender, . , , , Android . UDP- Android.

UDP :

public void sendMessage(String message)
{
    try {
        DatagramSocket ds = new DatagramSocket();
        DatagramPacket dp;
        InetAddress local = InetAddress.getByName(ipPool);
        dp = new DatagramPacket(message.getBytes(), message.length(), local, port);
        ds.setBroadcast(true);
        ds.send(dp);
        ds.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

UDP Android UDP-:

public class UDP_Server
{
    CommandParser commandParser;

    public UDP_Server(MainActivity mainActivity)
    {
        Log.i("Udp tutorial", "---------------------------Starting UDP SERVER");

        commandParser = new CommandParser(mainActivity);
        String text;
        int server_port = 9876;
        try
        {
            DatagramSocket s = new DatagramSocket(server_port);
            s.setBroadcast(true);
            //s.setReceiveBufferSize(163840);


            while (true)
            {
                byte[] message = new byte[1024];
                DatagramPacket p = new DatagramPacket(message, message.length);

                s.receive(p);
                text = new String(message, 0, p.getLength());
                Log.d("Udp tutorial","message:" + text);
                //commandParser.parseCommand(text);
                //s.close();
            }
        } catch (SocketException e)
        {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

UDPServer.java "onCreate()":

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        wm = (WifiManager) getSystemService(WIFI_SERVICE);

        Log.i("Udp tutorial", "---------------------------HERE 1");
        Thread thread = new Thread(new Runnable()
        {
            public void run()
            {
                UDP_Server svr = new UDP_Server(MainActivity.this);
            }
        });

        thread.setPriority(Thread.MAX_PRIORITY);
        thread.start();
//        TCPServer server = new TCPServer();
    }
+4
1

, Android TFTP ( UDP) Wi-Fi, , , Android.

( ) , ack ( ), , .

, , ( https://www.b4x.com/android/forum/threads/enable-multicast.43321/#post-263242)

WifiManager wifi = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock multicastLock = wifi.createMulticastLock("multicastLock");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();

...

multicastLock.release();

, .

0

Source: https://habr.com/ru/post/1664181/


All Articles