Android device (except Galaxy Note 2) cannot receive UDP packets

I am working on a client-side server application in which the server (a desktop program written in Java) sends screenshots via UDP to my Android device on the same wireless network. Of course, since the datagram size is larger than the standard udp size (65 K), I create pieces of them and loop them to my Android device using UDP.

My problem is that it works great on Samsung Galaxy Note 2 (Android 2.4.2), but it doesn’t work on other Android devices (even devices running the same version for Android. For Galaxy Tab 3, Galaxy Note 1). The code gets stuck in socket.receive (datagram) and doesn't even receive one packet.

Can someone please help me on this ???

public ClientListener(int port, int fps, AppDelegate del){
        delegate = del;
        framesPerSecond = fps;

        try{

            serverAddr = getLocalIpAddress();
            dgp = new DatagramPacket(buf, buf.length);

        }catch (Exception e){
            Log.e("ClientActivity", "C: Error", e);
        }
        serverPort = port;
    }



    public void run() {
           try {
               socket = new DatagramSocket(serverPort, serverAddr);
               connected = true;

               Timer timer = new Timer();
               int frames = 1000/framesPerSecond;

               Log.e("FRAMES", ""+frames);
               timer.scheduleAtFixedRate(getImageTask, 0, frames);

               listen();               
           }
           catch (Exception e) {
               Log.e("ClientActivity", "Client Connection Error", e);
           }
     }



private void listen()
    {

        while(connected){

            try{

                socket.receive(dgp); //gets stuck here on devices other than note 2




                byte[] data = dgp.getData();
                System.out.println("data received in datagram clientListener====="+data);
            /* Read header infomation */
            short session = (short) (data[1] & 0xff);
            short slices = (short) (data[2] & 0xff);
            int maxPacketSize = (int) ((data[3] & 0xff) << 8 | (data[4] & 0xff)); // mask

            short slice = (short) (data[5] & 0xff);
            int size = (int) ((data[6] & 0xff) << 8 | (data[7] & 0xff)); // mask



                System.out.println("------------- PACKET -------------");
                System.out.println("SESSION_START = "
                        + ((data[0] & SESSION_START) == SESSION_START));
                System.out.println("SSESSION_END = "
                        + ((data[0] & SESSION_END) == SESSION_END));
                System.out.println("SESSION NR = " + session);
                System.out.println("SLICES = " + slices);
                System.out.println("MAX PACKET SIZE = " + maxPacketSize);
                System.out.println("SLICE NR = " + slice);
                System.out.println("SIZE = " + size);
                System.out.println("------------- PACKET -------------\n");


            /* If SESSION_START falg is set, setup start values */
            if ((data[0] & SESSION_START) == SESSION_START) {
                if (session != currentSession) {
                    currentSession = session;
                    slicesStored = 0;
                    /* Consturct a appropreately sized byte array */
                    imageData = new byte[slices * maxPacketSize];
                    slicesCol = new int[slices];
                    sessionAvailable = true;
                }
            }

            /* If package belogs to current session */
            if (sessionAvailable && session == currentSession) {
                if (slicesCol != null && slicesCol[slice] == 0) {
                    slicesCol[slice] = 1;
                    System.arraycopy(data, HEADER_SIZE, imageData, slice
                            * maxPacketSize, size);
                    slicesStored++;
                }
            }

            /* If image is complete dispay it */
            if (slicesStored == slices) {
                ByteArrayInputStream bis = new ByteArrayInputStream(
                        imageData);
                 Bitmap bp=BitmapFactory.decodeStream(bis);
                         delegate.getController().setImage(bp);




                         Log.e("Testing", "Received image");






            }
+4
1

, . - Android . . , 2, . , .

0

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


All Articles