Search code examples
androidmulticastandroid-broadcastandroid-networking

Receive image (in Android) from multicast server


I have a PC-server that multicast a image/data. I want to develop a application in android phone to receive that image/data. Could you suggest to me the steps to do it? Thanks in advance


Solution

  • Since you ask for the steps. I present high level steps with some java pseudo code.

    MulticastSocket socket; 
    MulticastLock mcLock;
    WifiLock wifiLock;
    DatagramPacket dgram;
    
    
    
    // make a socket
    socket = new MulticastSocket(...)
    
    // join the multicast group
    socket.joinGroup( <your server> )
    
    // obtain wifi lock
    wifiLock = ((WifiManager) 
        THIS_CONTEXT.getSystemService(THIS_CONTEXT.WIFI_SERVICE)).createWifiLock("clientWifiLock");
    wifiLock.acquire();
    
    // obtain multicast lock
    mcLock = ((WifiManager) THIS_CONTEXT.getSystemService(Context.WIFI_SERVICE))
        .createMulticastLock("replicationMulticastLock");
    mcLock.acquire();
    
    // make a datagram packet for receiving
    dgram  = new DatagramPacket(...);
    
    // receive data
    socket.receive(dgram);
    // loop if necessary and receive more data and save it in a buffer
    

    You may also need the following permissions in your manifest file uses-permission android:name="android.permission.ACCESS_WIFI_STATE" uses-permission android:name="android.permission.CHANGE_WIFI_STATE" uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" uses-permission android:name="android.permission.INTERNET" uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"