简体   繁体   中英

Android UDP doesn't receive in some devices

I have an UDP send and receive which works in my device Samsung Galaxy Ace Plus (S7500) but the same code doesn't work in other devices, for example Samsung Galaxy S4. I don't have any error.

Send :

public class SendThread extends Thread {

byte[] receiveData = new byte[1024];
DatagramSocket serverSocket = null;


public SendThread() {
    this.start();
}

public void run() {
    DatagramSocket serverSocket = null;
    byte[] receiveData = new byte[1024];
    byte[] sendData = new byte[1024];

    try {
        serverSocket = new DatagramSocket("MY SOCKET PORT");
        InetAddress IP = InetAddress.getByName("MY IP");
        String send= "I am Android";
        sendData = send.getBytes();
        DatagramPacket send = new DatagramPacket(sendData, sendData.length, IP, "MY SEND PORT");
        serverSocket.send(send);

        serverSocket.close();
    } catch (Exception e) {
    }

}

}

Receive :

public class ReceiveThread extends Thread {

byte[] receiveData = new byte[1024];
DatagramSocket serverSocket = null;
boolean isActive = true;

public ReceiveThread() {
    this.start();
}

public void run() {

    DatagramSocket serverSocket = null;
    byte[] receiveData = new byte[1024];

    while (isActive) {
        try {
            serverSocket = new DatagramSocket("MY RECEIVE PORT");

            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            serverSocket.receive(receivePacket);
            String sentence = new String( receivePacket.getData());
            System.out.println("RECEIVED: " + sentence);

            serverSocket.close();

        } catch (Exception e){
        }
    }
}

}

This problem ocurred because some devices lock the Datagram receiver because the protocol security implemented by factory.

Your code is not wrong, but you need change the DatagramSocket for MulticastSocket.

For this your need execute some steps:

First, it's needed to add the uses-permission:

<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />

On AmdroidManifest.xml

Second, it's necessary create a MulticastLock; Without this the MulticastSocket is not work properly;

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
multicastLock = wifi.createMulticastLock("multicastLock");
multicastLock.setReferenceCounted(true);

Thirdy, replace the DatagramSocket by MulticastSocket. Only on receive methods was needed put the code below or similar:

MulticastSocket ms = new MulticastScoket("Your socket port");
ms.joinGroup("Your IP");

It's not needed any modifies to send messages.

I use the multcast ip equals to 239.255.255.255. Attempt to range of multicast ip because the wrong ip will block the method flow correctly.

Finally, before use MulticastSocket it's needed to execute MulticastLock.acquire() , and after use execute MulticastLock.release() ;

It could be puted on service, and acquire or release MulticastLock on start or stop service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM