Java – Android devices (except galaxy note 2) cannot get UDP packets

Android devices (except galaxy note 2) cannot get UDP packets… here is a solution to the problem.

Android devices (except galaxy note 2) cannot get UDP packets

I’m developing a client-server application where the server (a desktop program written in Java) sends screenshots via UDP to my android device in the same wireless network. Of course, since the datagram size is larger than the UDP standard size (65 K), I’m creating slices of them and sending them to my android device using UDP loops.

My problem is that it works perfectly on Samsung Galaxy Note 2 (android 4.2.2), but not on other android devices (even devices running the same android version for ex galaxy tab 3, galaxy Note 1). The code is stuck on socket.receive(datagram) and doesn’t even receive a single packet.

Can someone help me with 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");

}

Solution

Sorry everyone, my stupidity. Actually, for some strange reasons, the server cannot satisfy multiple connections from multiple android devices at the same time. Only one device should connect to the server or request a connection. In my case, I’ve logged in through my note 2 while trying to use another device. So my other devices can’t receive packets.

Related Problems and Solutions