从多播服务器接收图像(在Android中)

时间:2015-10-02 12:28:22

标签: android multicast android-broadcast android-networking

我有一台PC服务器,可以组播图像/数据。我想在Android手机中开发一个应用程序来接收该图像/数据。你能告诉我这样做的步骤吗?提前致谢

1 个答案:

答案 0 :(得分:1)

因为你要求步骤。我用一些java伪代码提出了高级步骤。

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

您可能还需要清单文件中的以下权限     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”